public class Java8Demo {
public static void main(String[] args) {
List<Student> studentList = Lists.newArrayList();
studentList.add(new Student(1, "張三", 10, "浙江杭州"));
studentList.add(new Student(2, "李四", 12, "江蘇南京"));
studentList.add(new Student(3, "王五", 15, "河南開封"));
studentList.add(new Student(4, "趙六", 18, "江西南昌"));
//提取id
List<Integer> idList = studentList.stream().map(Student::getId).collect(Collectors.toList());
System.out.println(idList);
//按照id分組
Map<Integer, List<Student>> integerListMap = studentList.stream().collect(Collectors.groupingBy(Student::getId));
System.out.println(integerListMap);
//按照id爲key,name爲value 分組
Map<Integer, String> integerStringMap = studentList.stream().collect(Collectors.toMap(Student::getId, Student::getName, (key1, key2) -> key2));
System.out.println(integerStringMap);
}
}
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Student {
private Integer id;
private String name;
private Integer age;
private String address;
}