Skip to content
java
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;
}

toMap(Function, Function, BinaryOperator, Supplier)

java
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

java
@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 + "加后缀";
}
{key1加后缀=val, key3加后缀=val, key2加后缀=val}

测试2

java
@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);
}
{key=val拼接逻辑val拼接逻辑val}

测试3

java
@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);
}
{key=val4}

测试4

java
@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 + "加后缀";
}
{key1加后缀=val1, 【map2】key1=【map2】val1, 【map2】key2=【map2】val2, key3加后缀=val3, key2加后缀=val2}
java

@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);
}

文章来源于自己总结和网络转载,内容如有任何问题,请大佬斧正!联系我