pursue wind pursue wind
首页
Java
Python
数据库
框架
Linux
中间件
前端
计算机基础
DevOps
项目
面试
书
关于
归档
MacOS🤣 (opens new window)
GitHub (opens new window)
首页
Java
Python
数据库
框架
Linux
中间件
前端
计算机基础
DevOps
项目
面试
书
关于
归档
MacOS🤣 (opens new window)
GitHub (opens new window)
  • 工具类

  • Java-集合框架

  • Java8

    • Java 遍历文件
    • Java8中的流操作-基本使用&性能测试
    • 使用CompletableFuture构建异步应用
    • Collectors.toMap()
      • CompletableFuture
    • Java-多线程

    • Java计时新姿势√
    • Java中的BlockingQueue
    • Lambda表达式被首次调用时很慢?从JIT到类加载再到实现原理
    • 正则表达式
    • Java定时任务
    • JavaWeb

    • Java
    • Java8
    pursuewind
    2020-11-23
    目录

    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

    # 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

    # 测试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
    {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
    {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
    {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
    {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
    Last Updated: 2023/01/30, 11:01:00
    使用CompletableFuture构建异步应用
    CompletableFuture

    ← 使用CompletableFuture构建异步应用 CompletableFuture→

    Theme by Vdoing | Copyright © 2019-2023 pursue-wind | 粤ICP备2022093130号
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式
    • 飙升榜
    • 新歌榜
    • 云音乐民谣榜
    • 美国Billboard榜
    • UK排行榜周榜
    • 网络DJ