文章目录
前言
Java集合Stream 出自 Java8。
可谓是加量不加价,丰富了使用场景,还精简了代码。虽然牺牲了一点可读性,但总体来说是很好用的。以下总结其中的一些用法供大家参考。
public class Student{
String name;
String sex;
String age;
Integer score;
}
一、List<对象>转List<字符串>
提取年龄集合
List<student> studentList = new ArrayList<>();
List<String> nameList = studentList.stream().map(student::getAge).collect(Collectors.toList());
提取年龄集合并去重
方式一
List<student> studentList = new ArrayList<>();
List<String> nameList = studentList.stream().map(student::getAge).distinct().collect(Collectors.toList());
方式二
Set<String> deptIds = studentList.stream().map(student::getAge).collect(Collectors.toSet());
二、按照某个字段 分组
提示:key为空时,报错
Map<String, List<Student>> map = studentList.stream().collect(Collectors.groupingBy(Student::getName));
三、List<对象>去重
方式一
List<student> studentList = studentList.stream().filter(student -> student.getAge() != null).collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Student::getAge))), ArrayList::new));
方式二 分组转map去重
Map<String, List<Student>> map = studentList.stream().collect(Collectors.groupingBy(Student::getName));
方式三 分组转map去重
Map<String, Student> collect = list.stream().collect(Collectors.toMap(Student::getName, Function.identity(),(key1,key2) -> key2));
四、 List<基础类型>去重
方式一
Set set = new HashSet<>();
List list = studentList.stream().filter(item -> set.add(item)).collect(Collectors.toList());
方式一延伸:如果需要取出重复数据
List list = studentList.stream().filter(item -> !set.add(item)).collect(Collectors.toList());
方式二
List distinctrs = studentList.stream().distinct().collect(Collectors.toList());
方式三
List distinctrs = studentList.stream().collect(Collectors.toSet());
五、filter匹配过滤
filter() 简单使用
对象匹配过滤
List<User> result1 = userList.stream().filter(v -> v.getName().equals("小明")).collect(Collectors.toList());
简单匹配过滤
List<String> result3 = list.stream().filter(v -> v.contains("A") && v.contains("C")).collect(Collectors.toList());
filter()与findFirst()、 orElse()、forEach()一起使用
User user = list.stream().filter(u -> u.getName().endsWith("sh")).findFirst().orElse(null);
list.stream().filter(u -> u.getName().endsWith("sh")).forEach(u -> System.out.println(u.getName()));
filter()与mapToInt()、sum()一起使用
int sum = list.stream().filter(u -> u.getId() >= 2 && u.getId() <= 4).mapToInt(u -> u.getAge()).sum();
Stream filter()与collect()一起使用
long count = list.stream().filter(u -> u.getName().endsWith("sh")).collect(Collectors.counting());
filter()与reduce()一起使用
list.stream().filter(u -> u.getName().endsWith("sh"))
.mapToInt(u -> u.getAge()).reduce((a, b) -> a + b)
.ifPresent(s -> System.out.println("Sum of age of users ending name with 'sh': "+ s));
六、List转Map
List 转成Map<String,String>
Map<String,String> map = list.stream().collect(Collectors.toMap(Student::getName,StudentDTO::getAge));
List 转成Map<String,Object>
方式一
Map<String, Student> collect = list.stream().collect(Collectors.toMap(Student::getName, student -> student));
方式二
Map<String, Student> collect = list.stream().collect(Collectors.toMap(Student::getName, Function.identity()));
方式二延伸
Map<String, User> map = list.stream().collect(Collectors.toMap(student -> student.getName() + user.getAge(), Function.identity()));
方式三: key重复时,用第三种方式,后key覆盖前key来解决
Map<String, Student> collect = list.stream().collect(Collectors.toMap(Student::getName, Function.identity(),(key1,key2) -> key2));
List 转成Map<String,List>
方式一
Map<String, List<Student>> map = studentList.stream().collect(Collectors.groupingBy(Student::getName));
方式二
Map<String, List<Student>> collect = list.stream().collect(Collectors.toMap(Student::getName, student -> Collections.singletonList(student)));
七、两个List集合取交集、并集、差集
List<String> list1 = new ArrayList<String>();
list1.add("1");
list1.add("2");
list1.add("3");
list1.add("5");
list1.add("6");
List<String> list2 = new ArrayList<String>();
list2.add("2");
list2.add("3");
list2.add("7");
list2.add("8");
// 交集
List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(toList());
System.out.println("---交集 intersection---");
intersection.parallelStream().forEach(System.out :: println);
// 差集 (list1 - list2)
List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(toList());
System.out.println("---差集 reduce1 (list1 - list2)---");
reduce1.parallelStream().forEach(System.out :: println);
// 差集 (list2 - list1)
List<String> reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(toList());
System.out.println("---差集 reduce2 (list2 - list1)---");
reduce2.parallelStream().forEach(System.out :: println);
// 并集
List<String> listAll = list1.parallelStream().collect(toList());
List<String> listAll2 = list2.parallelStream().collect(toList());
listAll.addAll(listAll2);
System.out.println("---并集 listAll---");
listAll.parallelStream().forEachOrdered(System.out :: println);
// 去重并集
List<String> listAllDistinct = listAll.stream().distinct().collect(toList());
System.out.println("---得到去重并集 listAllDistinct---");
listAllDistinct.parallelStream().forEachOrdered(System.out :: println);
System.out.println("---原来的List1---");
list1.parallelStream().forEachOrdered(System.out :: println);
System.out.println("---原来的List2---");
list2.parallelStream().forEachOrdered(System.out :: println);