JAVA stream流操作

1.根据某一项去重

List<String> collect = contactRelationList.stream().map(ContactRelation::getRelPersonId).distinct().collect(Collectors.toList());

 2.多条件去重

 List<ContactMethod> collect1 = contactMethodList1.stream().
                collect(collectingAndThen(toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getType() + ";" + o.getContactInfo()))), ArrayList::new));
3.list转map
 // 联系人转以id为key,名称为value的map
        Map<String, String> contactMap = contactList.stream().collect(Collectors.toMap(Contact::getId, Contact::getContact));

4. 以某一个条件做字符串逗号拼接

String pointPositionId = pointPositionList.stream().map(PointPosition::getId).collect(Collectors.joining(","));
5.使用Stream流把list1和list2根据属性menu合并一个list集合
// 使用Stream流把list1和list2根据属性menu合并一个list集合
List<QuotePlatformEntity> collect = platformList.stream().map(platform -> {
    quoteCommercialCategoryEntities.stream().filter(commercialCategoryEntity -> Objects.equals(platform.getCategoryId(), commercialCategoryEntity.getId())).forEach(commercialCategory -> platform.setCategory(commercialCategory.getName()));
    return platform;
}).collect(Collectors.toList());
6.  使用Stream流根据商业类别id分组
Map<String,List <QuotePlatformEntity>> collect1 = platformList.stream().collect(Collectors.groupingBy(QuotePlatformEntity::getCategoryId));

7.分组方式(根据对象中的某个字段的前6位):


Map<String, List<QuoteProjectEntity>> map = quoteProjectEntityList.stream().collect(
                Collectors.groupingBy(
                        quoteProjectEntity-> quoteProjectEntity.getCode().substring(0, 6)
                ));

8.使用 stream 从 List 对象中获取某列数据

//使用 stream 从 List 对象中获取某列数据

List<String> collect = list.stream().map(Student::getName).collect(Collectors.toList())


版权声明:本文为weixin_57209235原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。