Open superleeyom opened 4 years ago
假如有如下的一个数据结构:
[ { "userId": 1, "name": "王二狗", "className": "classA" }, { "userId": 2, "name": "李老四", "className": "classA" }, { "userId": 3, "name": "张翠花", "className": "classB" }, { "userId": 4, "name": "李雷", "className": "classB" } ]
需要将它按班级className进行分组,即如下的数据结构:
className
{ "classA": [ { "userId": 1, "name": "王二狗", "className": "classA" }, { "userId": 2, "name": "李老四", "className": "classA" } ], "classB": [ { "userId": 3, "name": "张翠花", "className": "classB" }, { "userId": 4, "name": "李雷", "className": "classB" } ] }
学生实体类 Student.java:
Student.java
@Data @AllArgsConstructor public class Student { private long userId; private String name; private String className; }
采用Java8函数式编程 groupingBy 语法进行快速分组:
groupingBy
Student s1 = new Student(1, "王二狗", "classA"); Student s2 = new Student(2, "李老四", "classA"); Student s3 = new Student(3, "张翠花", "classB"); Student s4 = new Student(4, "李雷", "classB"); List<Student> list = new ArrayList<>(); list.add(s1); list.add(s2); list.add(s3); list.add(s4); Map<String, List<Student>> map = list.stream().collect(Collectors.groupingBy(Student::getClassName)); System.out.println(JSONUtil.toJsonStr(map));
还是这个数据结构:
只不过要按用户的 userId 进行分组,分组后的数据格式如下:
userId
{ "1": { "className": "classA", "userId": 1, "name": "王二狗" }, "2": { "className": "classA", "userId": 2, "name": "李老四" }, "3": { "className": "classB", "userId": 3, "name": "张翠花" }, "4": { "className": "classB", "userId": 4, "name": "李雷" } }
采用Java8函数式编程的 toMap 语法进行快速分组:
toMap
Map<Long, Student> studentMap = list.stream().collect(Collectors.toMap(Student::getUserId, student -> student, (k1, k2) -> k1));
对于简单的集合,比如字符串,整型类的集合,采用 distinct 进行快速去重:
distinct
List<String> strList = Arrays.asList("a", "b", "c", "c", "d"); List<String> distinctList = strList.stream().distinct().collect(Collectors.toList()); System.out.println(JSONUtil.toJsonStr(distinctList));
对于集合元素是对象的,根据对象指定的属性进行去重:
// 根据className去重 List<Student> unique = list.stream().collect(Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Student::getClassName))), ArrayList::new)); System.out.println(JSONUtil.toJsonStr(unique));
// 根据userId和className去重 List<Student> unique2 = list.stream().collect(Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getUserId() + ";" + o.getClassName()))), ArrayList::new)); System.out.println(JSONUtil.toJsonStr(unique2));
Student s1 = new Student(1, "王二狗", "classA",10); Student s2 = new Student(2, "李老四", "classA",9); Student s3 = new Student(3, "张翠花", "classB",8); Student s4 = new Student(4, "李雷", "classB", 12); List<Student> list = new ArrayList<>(); list.add(s1); list.add(s2); list.add(s3); list.add(s4); // 按照年龄进行升序 list.sort(Comparator.comparing(Student::getAge)); // 按照年龄进行降序 list.sort(Comparator.comparing(Student::getAge).reversed());
// 查找学生当中是否存在一个叫王二狗的同学 boolean matchResult = list.stream().anyMatch(student -> "王二狗".equals(student.getName()));
List<String> cities = Arrays.asList("Milan", "London", "New York", "San Francisco"); String citiesCommaSeparated = String.join(",", cities); System.out.println(citiesCommaSeparated); // 输出: Milan,London,New York,San Francisco
分组(一对多)
假如有如下的一个数据结构:
需要将它按班级
className
进行分组,即如下的数据结构:学生实体类
Student.java
:采用Java8函数式编程
groupingBy
语法进行快速分组:分组(一对一)
还是这个数据结构:
只不过要按用户的
userId
进行分组,分组后的数据格式如下:采用Java8函数式编程的
toMap
语法进行快速分组:对集合中重复的元素进行去重
对于简单的集合,比如字符串,整型类的集合,采用
distinct
进行快速去重:对于集合元素是对象的,根据对象指定的属性进行去重:
对集合元素进行快速排序
快速判断对象集合中是否存在指定的元素
将List转变为逗号分隔的字符串
参考文章