1.轉成一對一的,一個id對應一個對象
Map<Long, User> maps = userList.stream().collect(Collectors.toMap(User::getId, Function.identity(), (key1, key2) -> key2)); 后面的key1,key2 是指定一種覆蓋規則,防止key沖突
2.如果想要得到某一屬性而不是 整個對象方法
Map<Long, String> maps = userList.stream().collect(Collectors.toMap(User::getId, User::getAge, (key1, key2) -> key2));
3.轉成一對多的,一個id對應多個對象
Map<Integer, List<Apple>> groupBy = appleList.stream().collect(Collectors.groupingBy(Apple::getId));