JAVA8-Lambda-groupingBy
使用场景:
例:有一群来自五湖四海的大学生,这些学生按照他们的家乡组建一场同乡会。
public static void main(String[] args) {
ArrayList<Student> list = new ArrayList<>();
list.add(new Student("张三", 19, "杭州"));
list.add(new Student("李四", 18, "海口"));
list.add(new Student("王五", 17, "西安"));
list.add(new Student("赵六", 16, "兰州"));
list.add(new Student("二狗", 16, "杭州"));
list.add(new Student("铁柱", 16, "海口"));
// groupingBy(分组条件)
Map<String, List<Student>> map = list.stream().collect(Collectors.groupingBy(Student::getAddr));
map.forEach((k, v) -> System.out.println(k + ":" + v));
}