1. Code
package com.xtm.java.test; import java.util.ArrayList; import java.util.Collection; import java.util.Comparator; import java.util.List; /** * Group implementation */ public class Test{ //entity class class Apple { String color; int weight; Apple(String color, int weight) { super(); this.color = color; this.weight = weight; } @Override public String toString() { return "Apple{" + "颜'" + color + '\'' + ", 重:" + weight + '}'; } } public static void main(String args[]){ List<Apple> list = new ArrayList<>(); list.add(new Test().new Apple("红", 205)); list.add(new Test().new Apple("绿", 248)); list.add(new Test().new Apple("红", 131)); list.add(new Test().new Apple("黄", 119)); list.add(new Test().new Apple("绿", 153)); list.add(new Test().new Apple("黄", 224)); // group by color List<List<Apple>> byColorList = byColorGroupApple(list, Comparator.comparing(t -> t.color)); printList(byColorList); // group by weight List<List<Apple>> byWeightList = byColorGroupApple(list, (t1, t2) -> t1.weight/100==t2.weight/100?0:1); printList(byWeightList); } private static void printList(List<List<Apple>> byColorList) { System.out.println("After grouping: "+byColorList); } /** * Group the collection according to the incoming comparator * @param data set of elements to be grouped * @param c incoming comparator * @param <T> element * @return returns the grouped list of sets */ private static <T> List<List<T>> byColorGroupApple(Collection<T> data, Comparator<? super T> c) {//Use generics on the method, remember to add <T> before the return value List<List<T>> result = new ArrayList<>(); for (T t : data) {//1. Loop out each element in the collection boolean isSameGroup = false;//2. The flag is not the same group for (List<T> aResult : result) {//4. Loop to find out whether the current element belongs to a created group if (c.compare(t, aResult.get(0)) == 0) {//aResult.get(0) means: as long as the current element and the first element of a certain group are compared through the comparator, they belong to this Group isSameGroup = true;//5. If it is found that the current element belongs to a certain group, set the flag bit to prevent it from creating a new group aResult.add(t);//6. Add the current element to the current group break; } } if (!isSameGroup) {//3. If it does not belong to any group, create a new group and add elements to the group // create List<T> innerList = new ArrayList(); innerList.add(t); result.add(innerList); } } return result; } }
Code 2: java8
package com.xtm.java.test; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import static java.util.stream.Collectors.groupingBy; public class Student { private int id; private String sex; private String name; public static void main(String[] args) { List<Student> list = new ArrayList(); list.add(new Student(1,"男","zhangsan")); list.add(new Student(2,"女","lisi")); list.add(new Student(3,"男","wangsu")); list.add(new Student(2,"女","zhaoliu")); list.add(new Student(1,"男","zhuoqi")); list.add(new Student(3,"女","cangkong")); // group all students by gender Map<String, List<Student>> collect = list.stream().collect(Collectors.groupingBy(Student::getSex)); // More readable with method references Map<String, List<Student>> collect2 = list.stream().collect(groupingBy(Student::getSex)); // group by id Map<Integer, List<Student>> collect3 = list.stream().collect(groupingBy(Student::getId)); //custom function grouping Map<Integer, List<Student>> collect4 = list.stream().collect(groupingBy(stu -> stu.hashCode() % 5)); System.out.println(collect); System.out.println(collect2); System.out.println(collect3); System.out.println(collect4); } public Student(int id, String sex, String name) { this.id = id; this.sex = sex; this.name = name; } public int getId() { return id; } public String getSex() { return sex; } public String getName() { return name; } @Override public String toString() { return "Student{" + "id=" + id + ", sex='" + sex + '\'' + ", name='" + name + '\'' + '}'; } }
2. Running results
After grouping: [[Apple{color 'red', weight: 205}, Apple{color 'red', weight: 131}], [Apple{color 'green', weight: 248}, Apple{color 'green', weight: 153}], [Apple{yan'huang', weight: 119}, Apple{yan'huang', weight: 224}]] After grouping: [[Apple{color 'red', weight: 205}, Apple{color 'green', weight: 248}, Apple{color 'yellow', weight: 224}], [Apple{color 'red', Weight: 131}, Apple{Yellow', weight: 119}, Apple{Yan'green', weight: 153}]]
{女=[Student{id=2, sex='女', name='lisi'}, Student{id=2, sex='女', name='zhaoliu'}], 男=[Student{id=1, sex='男', name='zhangsan'}, Student{id=3, sex='男', name='wangsu'}, Student{id=1, sex='男', name='zhuoqi'}, Student{id=3, sex='男', name='cangkong'}]} {女=[Student{id=2, sex='女', name='lisi'}, Student{id=2, sex='女', name='zhaoliu'}], 男=[Student{id=1, sex='男', name='zhangsan'}, Student{id=3, sex='男', name='wangsu'}, Student{id=1, sex='男', name='zhuoqi'}, Student{id=3, sex='男', name='cangkong'}]} {1=[Student{id=1, sex='男', name='zhangsan'}, Student{id=1, sex='男', name='zhuoqi'}], 2=[Student{id=2, sex='女', name='lisi'}, Student{id=2, sex='女', name='zhaoliu'}], 3=[Student{id=3, sex='男', name='wangsu'}, Student{id=3, sex='男', name='cangkong'}]} {0=[Student{id=2, sex='女', name='lisi'}, Student{id=2, sex='女', name='zhaoliu'}], 1=[Student{id=3, sex='男', name='wangsu'}], 2=[Student{id=1, sex='男', name='zhangsan'}, Student{id=1, sex='男', name='zhuoqi'}], 4=[Student{id=3, sex='男', name='cangkong'}]}