V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
zealinux
V2EX  ›  Java

Java 如何合并 List?

  •  
  •   zealinux · 2019-08-19 22:47:21 +08:00 · 5069 次点击
    这是一个创建于 1704 天前的主题,其中的信息可能已经有所发展或是发生改变。

    比如:

    A:[{"id": "A1", "count": 1}, {"id": "A3", "count": 2},...] B: [{"id": "A2", "count": 10}, {"id": "A3", "count": 10},...]

    合并 AB 成

    [{"id": "A1", "count": 1}, {"id": "A2", "count": 10}, {"id": "A3", "count": 12},...]

    即,如果有在 A,B 中,则 countA+countB


    有没有简单的方法, 我用了 N 个 for 循环,代码冗长。

    18 条回复    2019-08-20 17:04:08 +08:00
    daozhihun
        1
    daozhihun  
       2019-08-19 22:48:44 +08:00
    全部放到一个 hashSet 里面进行计数 ^o^
    daozhihun
        2
    daozhihun  
       2019-08-19 22:49:44 +08:00
    说错了,放在 Hashtable 里面计数。。
    lhx2008
        3
    lhx2008  
       2019-08-19 22:49:50 +08:00
    这种就是用 Map 就可以了,Map<String, Object>,String = A1/A2/A3 Object = {"id": "A3", "count": 2} 如果要保持有序用 LinkedListMap
    Magentaize
        4
    Magentaize  
       2019-08-19 22:51:25 +08:00 via iPhone
    groupby
    chendy
        5
    chendy  
       2019-08-19 22:54:18 +08:00   ❤️ 2
    ```java
    List<Something> result = Stream.of(listA, listB)
    .flatMap(Collection::stream)
    .collect(Collectors.groupingBy(t -> t.getId(), Collectors.summingInt(x -> x.getCount())))
    .entrySet().stream().map(entry -> new Something(entry.getKey(), entry.getValue()))
    .collect(Collectors.toList());
    ```
    zhazi
        6
    zhazi  
       2019-08-19 23:03:44 +08:00
    reduce
    lastpass
        7
    lastpass  
       2019-08-19 23:25:42 +08:00 via Android
    当然是直接使用 map 进行合并。继承如 hashmap 类,并且重写其 put 方法。在 put 的之前先检验,累加。
    →_→然后再 new ArrayList(map.values())转换回 list。
    zealinux
        8
    zealinux  
    OP
       2019-08-19 23:28:14 +08:00
    @chendy

    这语法虽然看起来有些怪异。
    但本能觉得应该是优异的。👍

    有点不想再写 getId(),getCount(),hashMap 都是简单得 put 进去的。
    A 和 B 不一定是相同结构的,K/V 数量可能不一致。
    lastpass
        9
    lastpass  
       2019-08-19 23:36:11 +08:00 via Android
    当然,如果你要稍微优雅一点,可以将 hashmap 的继承类写成匿名 /私有 内部类。
    或者按照 JAVA 最正统也最优雅但也最费事儿的写法。是自己去实现一个 list 接口。自己现实 addAll 方法。实现取并集。→_→
    lqw3030
        10
    lqw3030  
       2019-08-20 08:34:19 +08:00
    zealinux
        11
    zealinux  
    OP
       2019-08-20 09:33:58 +08:00
    @lqw3030 不行,addAll 只会无脑拼接。
    gonethen
        12
    gonethen  
       2019-08-20 09:42:44 +08:00
    private static void removeDuplicate(List<String> list) {
    LinkedHashSet<String> set = new LinkedHashSet<>(list.size());
    set.addAll(list);
    list.clear();
    list.addAll(set);
    }
    cigarzh
        13
    cigarzh  
       2019-08-20 09:57:54 +08:00
    groupby 之后再 sum
    Raymon111111
        14
    Raymon111111  
       2019-08-20 10:45:13 +08:00
    就是放进 map 然后转回来
    zifangsky
        15
    zifangsky  
       2019-08-20 11:37:00 +08:00
    给你写了一份测试代码,我试了下没毛病。
    zhady009
        16
    zhady009  
       2019-08-20 13:47:22 +08:00
    Rwing
        17
    Rwing  
       2019-08-20 16:18:39 +08:00
    var list1 = new List<Item> { new Item { Id = "A1", Count = 2 }, new Item { Id = "A2", Count = 2 } };
    var list2 = new List<Item> { new Item { Id = "A3", Count = 2 }, new Item { Id = "A2", Count = 10 } };
    var list3 = list1.Concat(list2)
    .GroupBy(i => i.Id)
    .Select(g => new Item() { Id = g.Key, Count = g.Sum(i => i.Count) })
    .ToList();


    C# 欢迎你...
    yyConstantine
        18
    yyConstantine  
       2019-08-20 17:04:08 +08:00
    @lhx2008 那 Object 里的 count 怎么取出呢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3586 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 45ms · UTC 04:34 · PVG 12:34 · LAX 21:34 · JFK 00:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.