阿超
>
使用BiFunction更改map的value
伟大变为可笑只有一步,但再走一步,可笑又会变为伟大。——佩思
这个PR
是这样的:
map
通过传入的BiFunction实现来返回值为新的map,支持返回别的类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
public static <K, V, R> Map<K, R> map(Map<K, V> map, BiFunction<K, V, R> biFunction) { if (null == map || null == biFunction) { return MapUtil.newHashMap(); } return map.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, m -> biFunction.apply(m.getKey(), m.getValue()))); }
|
定义个小枚举
1
| enum PeopleEnum {GIRL, BOY, CHILD}
|
开始操作
1 2 3 4 5 6 7
| Map<Integer, String> adjectivesMap = MapUtil.<Integer, String>builder() .put(0, "lovely") .put(1, "friendly") .put(2, "happily") .build();
Map<Integer, String> resultMap = MapUtil.map(adjectivesMap, (k, v) -> v + " " + PeopleEnum.values()[k].name().toLowerCase());
|
结果为
1 2 3 4 5
| { 0: "lovely girl", 1: "friendly boy", 2: "happily child" }
|
复杂一点的例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| @Data @Builder public static class User { private Long id; private String name; }
@Data @Builder public static class Group { private Long id; private List<User> users; }
@Data @Builder public static class UserGroup { private Long userId; private Long groupId; }
|
操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| Queue<String> customers = new ArrayDeque<>(Arrays.asList("刑部尚书手工耿", "木瓜大盗大漠叔", "竹鼠发烧找华农", "朴实无华朱一旦"));
List<Group> groups = Stream.iterate(0L, i -> ++i).limit(4).map(i -> Group.builder().id(i).build()).collect(Collectors.toList());
Map<Long, User> idUserMap = Stream.iterate(0L, i -> ++i).limit(4).map(i -> User.builder().id(i).name(customers.poll()).build()).collect(Collectors.toMap(User::getId, Function.identity()));
Map<Long, List<Long>> groupIdUserIdsMap = groups.stream().flatMap(group -> idUserMap.keySet().stream().map(userId -> UserGroup.builder().groupId(group.getId()).userId(userId).build())).collect(Collectors.groupingBy(UserGroup::getUserId, Collectors.mapping(UserGroup::getGroupId, Collectors.toList())));
Map<Long, List<User>> groupIdUserMap = MapUtil.map(groupIdUserIdsMap, (groupId, userIds) -> userIds.stream().map(idUserMap::get).collect(Collectors.toList()));
groups.forEach(group -> Opt.ofNullable(group.getId()).map(groupIdUserMap::get).ifPresent(group::setUsers));
groups.forEach(group -> { List<User> users = group.getUsers(); Assert.assertEquals("刑部尚书手工耿", users.get(0).getName()); Assert.assertEquals("木瓜大盗大漠叔", users.get(1).getName()); Assert.assertEquals("竹鼠发烧找华农", users.get(2).getName()); Assert.assertEquals("朴实无华朱一旦", users.get(3).getName()); });
|