stream流常用方法

去重
例:根据lists中的对象的empContId属性去重

List<String>  ids= lists.stream().map(e -> e.empContId).distinct().collect(Collectors.toList());

例:根据对象中的某个属性去重,并且去掉那个创建时间久的那个,然后返回对象集合

        List<ResourceImage> resourceImages = resourceImageRepository.findAll(specification);
        //根据镜像名称去重,保留时间最新的
        Map<String, Optional<ResourceImage>> collect = resourceImages.stream().collect(Collectors.groupingBy(ResourceImage::getImageName, Collectors.maxBy(Comparator.comparing(ResourceImage::getCreateTime))));
        //返回前五十条
        return collect.values().stream().map(e -> e.get()).sorted(Comparator.comparing(ResourceImage::getCreateTime).reversed()).limit(50).collect(Collectors.toList());

分组
例:根据lits中的对象的属性contid分组

Map<Integer, List<EmployeeResp>> collect = lits.stream().collect(Collectors.groupingBy(EmployeeResp::getEmpContId));

拼接ids
例:将lists中的对象的cityId拼接,按逗号分割

	String cityids = lists.stream().map(e -> e.cityId).collect(Collectors.joining(","));

计数

   long count = lists.stream().filter(e -> e.length() > 4).count();

转Map

//取某个属性
    Map<String, String> billMap = financeBillbases.stream()        .collect(Collectors.toMap(FinanceBillbase::getId,FinanceBillbase::getBlbsName));
//取某个对象
    Map<String, Company> billMap = = comps.stream().collect(Collectors.toMap(Company::getId, Company -> Company));
解决重复kay问题

报错信息:Duplicate key - (attempted merging values
1.后值覆盖前值

   Map<String, Company> billMap = = comps.stream().collect(Collectors.toMap(Company::getId, Company -> Company), (entity1, entity2) -> entity1));

2.后值累加前值或处理

Map<String, String> map = list.stream().collect(Collectors.toMap(Student :: getClassName, Student :: getStudentName, 
	(key1 , key2)-> key1 + "," + key2 ));

3.按重复的key再分组处理:

Map<String, List<String>> map = list.stream().collect(Collectors.toMap(Student :: getClassName, 
   // 此时的value 为集合,方便重复时操作
   s ->  {
   List<String> studentNameList = new ArrayList<>();
   studentNameList.add(s.getStudentName());
   return studentNameList;
   }, 
   // 重复时将现在的值全部加入到之前的值内
   (List<String> value1, List<String> value2) -> {
   value1.addAll(value2);
   return value1;
   }
));

输出:
{一年级三班=[翠花, 香兰], 一年级二班=[小明, 小芳, 小华]}

过滤

List<Student> collect = list.stream().filter(e->e.riskEmpid!=null).collect(Collectors.toList());

返回元素

//        //返回任意一个元素(串行的一般都拿第一个了,可以使用并行流,就是拿的任意的)
//        //串行流
        ResourceImage resourceImage1 = resourceImages.stream().findAny().get();
//        //并行流
        ResourceImage resourceImage3 = resourceImages.parallelStream().findAny().get();
//        //返回第一个元素
        ResourceImage resourceImage2 = resourceImages.stream().findFirst().get();

匹配元素

//        //检查是否至少匹配一个元素
        boolean b1 = resourceImages.stream().anyMatch(e ->{
            return e.getPermissions().equals("1")||e.getImageName().equalsIgnoreCase("1");
        });
//        //检查是否匹配所有元素
        boolean b2 = resourceImages.stream().allMatch(e -> e.getPermissions().equals("1"));
//        //反向匹配
        boolean b3 = resourceImages.stream().noneMatch(e -> e.getPermissions().equals("1"));

flatMap
flatMap是扁平化处理,可以将集合的多个值放入到一个集合然后进行处理
例:

    @Test
    public void test(){
        List<String> stringList = Arrays.asList("hello", "world");
        List<String> collect = stringList.stream()
                .map(str -> str.split(""))
                .flatMap(Arrays::stream)
                .distinct()
                .collect(Collectors.toList());
        System.out.println(collect);
    }

在这里插入图片描述
并行流 parallelStream

public class ParallelStreamTest02 {
    public static void main(String[] args) {
        //并行流的两种获取方式
        List<Integer> list = new ArrayList<>();
        Stream<Integer> integerStream = list.parallelStream();
 
        Stream<Integer> parallel = Stream.of(5, 8, 4, 6, 9).parallel();
    }
}

获取第一个元素,获取最后一个元素

       Date startTime = parse.stream().sorted().reduce((first, second) -> first).orElse(null);
        Date endTime = parse.stream().sorted().reduce((first, second) -> second).orElse(null);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宝藏-程序男孩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值