Collectors.toMap()
public Map<String, Map<String, List<Article>>> filedArticle() {
TreeMap<Integer, Map<Integer, List<Article>>> res = new TreeMap<>((x, y) -> y - x);
articleMapper.selectList(
new QueryWrapper<Article>()
.orderByDesc(Article.COL_CREATE_TIME)
//文章不是私有,也不是草稿
.and(i -> i.eq(Article.COL_IS_PRIVATE, false)
.eq(Article.COL_IS_DRAFT, false)))
.stream()
.peek(article -> article.setReadPassword(StringUtils.isBlank(article.getReadPassword()) ? "" : "密"))
//按年归档
.collect(Collectors.groupingBy(article -> {
Calendar calendar = Calendar.getInstance();
calendar.setTime(article.getCreateTime());
return calendar.get(Calendar.YEAR);
}, () -> new TreeMap<>((x, y) -> y - x), Collectors.toList()))
//按月归档
.forEach((k, v) -> {
res.put(k, v.stream().collect(Collectors.groupingBy(article -> {
Calendar calendar = Calendar.getInstance();
calendar.setTime(article.getCreateTime());
return calendar.get(Calendar.MONTH);
}, () -> new TreeMap<>((x, y) -> y - x), Collectors.toList())));
});
return res.entrySet()
.stream()
.collect(Collectors.toMap(transformMapKey2String("年").compose(Map.Entry::getKey),
val -> val.getValue().entrySet()
.stream()
.collect(Collectors.toMap(
transformMapKey2String("月").compose(Map.Entry::getKey), Map.Entry::getValue))));
}
private Function<Integer, String> transformMapKey2String(String suffix) {
return key -> key + suffix;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# toMap(Function, Function, BinaryOperator, Supplier)
public static <T, K, U, M extends Map<K, U>>
Collector<T, ?, M> toMap(Function<? super T, ? extends K> keyMapper,
Function<? super T, ? extends U> valueMapper,
BinaryOperator<U> mergeFunction,
Supplier<M> mapSupplier)
1
2
3
4
5
2
3
4
5
# 测试1
@Test
public void testMapTransformKey() {
Map<String, String> map = new HashMap<>();
IntStream.range(1, 4).forEach(i -> map.put("key" + i, "val" + i));
Map<String, String> collect = map.entrySet()
.stream()
.collect(Collectors.toMap(
castKey().compose(Map.Entry::getKey),
val -> val.getValue().substring(0, 3),
(s, a) -> s + "拼接逻辑" + a
));
System.out.println(collect);
}
private Function<String, String> castKey() {
return key -> key + "加后缀";
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{key1加后缀=val, key3加后缀=val, key2加后缀=val}
1
# 测试2
@Test
public void testMapTransformKey() {
Map<String, String> map = new HashMap<>();
IntStream.range(1, 4).forEach(i -> map.put("key" + i, "val" + i));
Map<String, String> collect = map.entrySet()
.stream()
.collect(Collectors.toMap(
castKey().compose(Map.Entry::getKey),
val -> val.getValue().substring(0, 3),
(s, a) -> s + "拼接逻辑" + a
));
System.out.println(collect);
}
private Function<String, String> castKey() {
return key -> key.substring(0, 3);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{key=val拼接逻辑val拼接逻辑val}
1
# 测试3
@Test
public void testMapTransformKey() {
Map<String, String> map = new HashMap<>();
IntStream.range(1, 4).forEach(i -> map.put("key" + i, "val" + i));
Map<String, String> collect = map.entrySet()
.stream()
.collect(Collectors.toMap(
castKey().compose(Map.Entry::getKey),
Map.Entry::getValue,
(s, a) -> null
));
System.out.println(collect);
}
private Function<String, String> castKey() {
return key -> key.substring(0, 3);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{key=val4}
1
# 测试4
@Test
public void testMapTransformKey() {
Map<String, String> map = new HashMap<>();
IntStream.range(1, 4).forEach(i -> map.put("key" + i, "val" + i));
Map<String, String> map2 = new HashMap<>();
IntStream.range(1, 3).forEach(i -> map2.put("【map2】key" + i, "【map2】val" + i));
Map<String, String> collect = map.entrySet()
.stream()
.collect(Collectors.toMap(
castKey().compose(Map.Entry::getKey),
Map.Entry::getValue,
(s, a) -> null,
() -> map2
));
System.out.println(collect);
}
private Function<String, String> castKey() {
return key -> key + "加后缀";
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{key1加后缀=val1, 【map2】key1=【map2】val1, 【map2】key2=【map2】val2, key3加后缀=val3, key2加后缀=val2}
1
@Data
@Accessors(chain = true)
class User {
private int userId;
private String tlId;
}
@Test
public void testMapTransformKey() {
List<User> users = Lists.newArrayList(
new User().setUserId(1).setTlId("2"),
new User().setUserId(1).setTlId("1"),
new User().setUserId(1).setTlId("3"),
new User().setUserId(2).setTlId("4"),
new User().setUserId(2).setTlId("5")
);
Map<Integer, String> collect = users
.stream()
.collect(Collectors.toMap(
t -> t.getUserId(),
u -> u.getTlId(),
(s, a) -> s + a
));
System.out.println(collect);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Last Updated: 2023/01/30, 11:01:00