一般简单方式我们会使用双重for循环来处理判断数据取值(如下代码所示),但是数据量越大的情况下代码效率则越低,并且现在很多公司都会限制for循环层数所以更推荐stream将List转换为Map通过key取值
普通方式:
- for (DashboardsDO aDo : dashboardsDOS) {
- for (Target target : targetList) {
- if (aDo.getStoreCode().equals(target.getStoreCode())) {
- 代码逻辑等等
- }
- }
- }
stream:
- List
list1= new ArrayList<>(); - List
list2= new ArrayList<>(); - // 假设以上两个list都有数据我们通过学生姓名name与学号num进行取值
- Map
studentMap=list2.stream() - .collect(Collectors.toMap(studentMSG-> studentMSG.getName() + "_" + studentMSG.getNum(), Function.identity()));
- // 此时我们将name与num作为map的key,值为当前实体类
- list1 = list1.stream()
- .map(student-> {
- // 将list1的name与num作为key
- String key = student.getName() + "_" + student.getNum();
- // 通过key取值
- StudentMSG studentMSG = studentMap.get(key);
- // 你的业务逻辑
- return student;
- }).collect(Collectors.toList());
- List
numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); -
- // 使用Stream流过滤出偶数并求和
- int sumOfEvenNumbers = numbers.stream()
- .filter(n -> n % 2 == 0)
- .mapToInt(Integer::intValue)
- .sum();
- System.out.println("Sum of even numbers: " + sumOfEvenNumbers);
- List
numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); - // 使用Stream流将数字转换为字符串并连接成一个字符串
- String joinedNumbers = numbers.stream()
- .map(String::valueOf)
- .collect(Collectors.joining(", "));
- System.out.println("Joined numbers: " + joinedNumbers);
- List
people = new ArrayList<>(); - people.add(new Person("张三", 30));
- people.add(new Person("李四", 25));
- people.add(new Person("王五", 35));
- // 根据age字段排序
- List
sortedPeople = people.stream() - .sorted(Comparator.comparingInt(Person::getAge))
- .collect(Collectors.toList());
-
- sortedPeople.forEach(person -> System.out.println(person.getName() + ": " + person.getAge()));
- List
people = new ArrayList<>(); - people.add(new Person("张三", 20));
- people.add(new Person("李四", 15));
- people.add(new Person("王五", 25));
- // 过滤年龄大于18岁的人
- List
adults = people.stream() - .filter(person -> person.getAge() >= 18)
- .collect(Collectors.toList());
-
- adults.forEach(person -> System.out.println(person.getName() + ": " + person.getAge()));
- List
numbers = Arrays.asList(1, 2, 3, 4, 5, 1, 2, 3); - List
distinctNumbers = numbers.stream().distinct().collect(Collectors.toList());
- List
list1 = new ArrayList<>(); - list1.add(new MyClass("A"));
- list1.add(new MyClass("B"));
- list1.add(new MyClass("C"));
-
- List
list2 = new ArrayList<>(); - list2.add(new MyClass("B"));
- list2.add(new MyClass("C"));
- list2.add(new MyClass("D"));
- // 根据field字段去重
- List
result = Stream.concat(list1.stream(), list2.stream()) - .distinct()
- .collect(Collectors.toList());
-
- for (MyClass item : result) {
- System.out.println(item.getField());
- }
- List
strings = Arrays.asList("apple", "banana", "cherry"); - List
lengths = strings.stream() - .map(String::length)
- .collect(Collectors.toList());
- System.out.println(lengths); // 输出 [5, 6, 6]
- List
strings = Arrays.asList("apple", "banana", "cherry", "date"); - List
sortedStrings = strings.stream() - .sorted((s1, s2) -> Integer.compare(s1.length(), s2.length()))
- .collect(Collectors.toList());
- System.out.println(sortedStrings); // 输出 [date, apple, cherry, banana]