import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import java.util.stream.Stream; public class TeatLamada { public static void main(String[] args) { Map<Integer, List<Student>> map = Stream.iterate(1, i -> i + 1).limit(10).map(v -> { Student stu = new Student(); stu.setId(v); stu.setYear(1992); stu.setAge(v % 2); return stu; }).collect(Collectors.groupingBy(x -> x.getYear())); map.entrySet().stream().forEach(System.out::println); List<Student> list = Stream.iterate(1, i -> i + 1).limit(10).map(i -> { Student stu = new Student(); stu.setAge(i % 2); stu.setId(i); stu.setYear(1992 + i % 2); return stu; }).collect(Collectors.toList()); list.stream().collect(Collectors.groupingBy( v -> new ArrayList<Integer>(Arrays.asList(v.getYear(),v.getAge())) )).entrySet().stream().forEach(System.out::println); list.stream().collect(Collectors.groupingBy( v -> new ArrayList(Arrays.asList(v.getYear(),v.getAge())),Collectors.summarizingInt(Student::getAge))) .entrySet().stream().forEach(System.out::println); } }
java 中Stream使用groupby记录
最新推荐文章于 2024-07-22 22:22:52 发布