- 重复键的去除。
Map<Long, Long> restaurantMap = restaurants.stream().collect(Collectors.toMap(Restaurant::getElemeRestaurantId, Restaurant::getStoreId, (key1, key2) -> key2));
- List 以ID分组 Map<Integer,List<Apple>>
Map<Integer, List<Apple>> groupBy = appleList.stream().collect(Collectors.groupingBy(Apple::getId));
- List以ID分组后聚合对象的一个属性
Map<Long, List<Long>> restaurantMap = restaurants.stream().collect(Collectors.toMap(
Restaurant::getStoreId,
r -> Collections.singletonList(r.getElemeRestaurantId()),
(x, y) -> {
List<Long> l = new ArrayList<>();
l.addAll(y);
return l;
}
));
网友评论