Java8特性:分组、提取字段、去重、过滤、差集、交集

总结下自己使用过的特性

  • 将对象集合根据某个字段分组
java 复制代码
//根据id分组
Map<String, List<Bean>> newMap = successCf.stream().collect(Collectors.groupingBy(b -> b.getId().trim()));
  • 获取对象集合里面的某个字段的集合
java 复制代码
List<Bean> list = new ArrayList<>(); 
List<String> strList = list.stream().map(Bean::getId).collect(Collectors.toList());
  • 將集合字段转换成字符串
java 复制代码
List<Long> longList = new ArrayList<>();
String s = longList.stream().map(Object :: toString).collect(Collectors.joining(","));
  • 提取字段去重distinct
java 复制代码
List<String> strList = list.stream().map(Bean::getId).distinct().collect(Collectors.toList());
  • 提取字段去重set
java 复制代码
Set<String> idSet = list.stream().map(Bean :: getId).collect(Collectors.toSet());
  • 过滤filter
java 复制代码
Map<String, Integer> map = new HashMap<>();
//先分组
Map<String, List<Bean>> maps = list.stream().collect(Collectors.groupingBy(Bean::getname));
//循环获取到大小
houseIdMaps.forEach((s, names) -> {
            //房屋地址对应的条数
            map.put(s, names.size());
});
//过滤
//过滤重复的数据 >1
Map<String, Integer> mapRepeat = map.entrySet().stream().filter(entry -> entry.getValue() > 1).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
//过滤未重复的数据 =1
Map<String, Integer> mapNoRepeat = map.entrySet().stream().filter(entry -> entry.getValue() == 1).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
  • 过滤filter,equals
java 复制代码
List<Bean> list1 = list.stream().filter(a -> a.getId().equals("1")).collect(Collectors.toList());
  • 差集(基于java8新特性) 适用于大数据量
java 复制代码
    /**
     * 差集(基于java8新特性) 适用于大数据量
     * 求List1中有的但是List2中没有的元素
     */
    public static List<String> subList(List<String> list1, List<String> list2) {
        Map<String, String> tempMap = list2.parallelStream().collect(Collectors.toMap(Function.identity(), Function.identity(), (oldData, newData) -> newData));
        return list1.parallelStream().filter(str-> !tempMap.containsKey(str)).collect(Collectors.toList());
    }
  • 交集(基于java8新特性) 适用于大数据量
java 复制代码
    /**
     * 交集(基于java8新特性) 适用于大数据量
     * 求List1和List2中都有的元素
     */
    public static List<String> intersectList(List<String> list1, List<String> list2){
        Map<String, String> tempMap = list2.parallelStream().collect(Collectors.toMap(Function.identity(), Function.identity(), (oldData, newData) -> newData));
        return list1.parallelStream().filter(str-> tempMap.containsKey(str)).collect(Collectors.toList());
    }
相关推荐
骑士雄师19 分钟前
Java 泛型中级面试题及答案
java·开发语言·面试
.格子衫.6 小时前
Spring Boot 原理篇
java·spring boot·后端
多云几多6 小时前
Yudao单体项目 springboot Admin安全验证开启
java·spring boot·spring·springbootadmin
Jabes.yang8 小时前
Java求职面试实战:从Spring Boot到微服务架构的技术探讨
java·数据库·spring boot·微服务·面试·消息队列·互联网大厂
聪明的笨猪猪9 小时前
Java Redis “高可用 — 主从复制”面试清单(含超通俗生活案例与深度理解)
java·经验分享·笔记·面试
兮动人9 小时前
Spring Bean耗时分析工具
java·后端·spring·bean耗时分析工具
MESSIR229 小时前
Spring IOC(控制反转)中常用注解
java·spring
摇滚侠9 小时前
Spring Boot 3零基础教程,Demo小结,笔记04
java·spring boot·笔记
笨手笨脚の10 小时前
设计模式-迭代器模式
java·设计模式·迭代器模式·行为型设计模式
spencer_tseng10 小时前
Eclipse 4.7 ADT (Android Development Tools For Eclipse)
android·java·eclipse