【MapSheep】
[好记性不如烂笔头]
public static void main(String[] args) {
	List<Student> list = new ArrayList<>();
	Collections.addAll(list, new Student(1L, "张三"), new Student(2L, "李四"), new Student(3L, "闫新蕊"));

	Map<Long, List<Student>> orderItemMap = list.stream().collect(Collectors.toMap(Student::getId, e -> Lists.newArrayList(e),
			(List<Student> oldList, List<Student> newList) -> {
				oldList.addAll(newList);
				return oldList;
			}));

	System.out.println(orderItemMap);
        //TODO {1=[Student(id=1, name=张三)], 2=[Student(id=2, name=李四)], 3=[Student(id=3, name=闫新蕊)]}

	//适用于1年级外键关联id为1的张三, 查询出张三
	Long firstGrade = 1L;
	System.out.println(orderItemMap.get(firstGrade));
        //TODO [Student(id=1, name=张三)]
}
posted on 2021-05-12 10:24  (Play)  阅读(62)  评论(0编辑  收藏  举报