一、List转Map
Map<String, String> collect = list.stream().collect(Collectors.toMap(UserEntity::getUserName, UserEntity::getEmail));
二、List转List
List<String> collect = userList.stream().map(UserEntity::getuserName).collect(Collectors.toList());
三、分组
Map<String, List<UserEntity>> listMap = list.stream().collect(Collectors.groupingBy(UserEntity::getUserName));
for(String key:listMap.keySet()){
System.out.print("姓名:"+key);
listMap.get(key).forEach(UserEntity -> System.out.print(UserEntity.getEmail()));
System.out.println();
}
四、排序
list.stream().sorted(Comparator.comparing(UserEntity-> UserEntity.getAge()))
.forEach(UserEntity -> System.out.println(UserEntity.getUserName()));
五、过滤
list.stream().filter(UserEntity -> UserEntity.getSex().equals("男")).collect(Collectors.toList())
.forEach(UserEntity -> System.out.println(UserEntity.getUserName()));
六、多条件去重
list.stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(
Comparator.comparing(UserEntity -> UserEntity.getAge() + ";" + UserEntity.getUserName()))), ArrayList::new))
.forEach(UserEntity -> System.out.println(UserEntity.getUserName()));
七、最小值
Integer min = list.stream().mapToInt(UserEntity::getAge).min().getAsInt();
七、最大值
Integer max = list.stream().mapToInt(UserEntity::getAge).max().getAsInt();
八、平均值
Double average = list.stream().mapToInt(UserEntity::getAge).average().getAsDouble();
八、求和
Integer sum = list.stream().mapToInt(UserEntity::getAge).sum();
九、分组求和
Map<String, IntSummaryStatistics> collect = list.stream().collect(Collectors.groupingBy(UserEntity::getSex, Collectors.summarizingInt(UserEntity::getAge)));
IntSummaryStatistics statistics1 = collect.get("男");
IntSummaryStatistics statistics2 = collect.get("女");
System.out.println(statistics1.getSum());
System.out.println(statistics1.getAverage());
System.out.println(statistics1.getMax());
System.out.println(statistics1.getMin());
System.out.println(statistics1.getCount());
System.out.println(statistics2.getSum());
System.out.println(statistics2.getAverage());
System.out.println(statistics2.getMax());
System.out.println(statistics2.getMin());
System.out.println(statistics2.getCount());
十、将 List元素存储到数组中
List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
int[] arr = list.stream().mapToInt(Integer::intValue).toArray();
十一、将数组元素存储到 List 中
int[] arr = {1, 2, 3, 4, 5};
List<Integer> list = IntStream.of(arr).boxed().collect(Collectors.toList());
十二、两个集合间的交集
List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(Collectors.toList());
十三、两个集合间的差集 (list1 - list2)
List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(Collectors.toList());
十四、两个集合间的差集 (list2 - list1)
List<String> reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(Collectors.toList());
十五、两个集合间的并集
List<String> listAll = list1.parallelStream().collect(Collectors.toList());
List<String> listAll2 = list2.parallelStream().collect(Collectors.toList());
listAll.addAll(listAll2);
十六、两个集合间的去重并集
List<String> listAllDistinct = listAll.stream().distinct().collect(Collectors.toList());
十七、两个集合间的根据id去重
allRoleList.stream().filter(distinctByKey(e -> e.getId())).collect(Collectors.toList());
十八、Set
Set<String> collect = userList.stream().map(UserEntity::getuserName).collect(Collectors.toSet());
未完待续......
本文共 135 个字数,平均阅读时长 ≈ 1分钟
评论 (0)