zealinux
V2EX  ›  Java

Java 如何合并 List?

  •  
  •   zealinux · Aug 19, 2019 · 5693 views
    This topic created in 2489 days ago, the information mentioned may be changed or developed.

    比如:

    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 replies    2019-08-20 17:04:08 +08:00
    daozhihun
        1
    daozhihun  
       Aug 19, 2019
    全部放到一个 hashSet 里面进行计数 ^o^
    daozhihun
        2
    daozhihun  
       Aug 19, 2019
    说错了,放在 Hashtable 里面计数。。
    lhx2008
        3
    lhx2008  
       Aug 19, 2019
    这种就是用 Map 就可以了,Map<String, Object>,String = A1/A2/A3 Object = {"id": "A3", "count": 2} 如果要保持有序用 LinkedListMap
    Magentaize
        4
    Magentaize  
       Aug 19, 2019 via iPhone
    groupby
    chendy
        5
    chendy  
       Aug 19, 2019   ❤️ 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  
       Aug 19, 2019
    reduce
    lastpass
        7
    lastpass  
       Aug 19, 2019 via Android
    当然是直接使用 map 进行合并。继承如 hashmap 类,并且重写其 put 方法。在 put 的之前先检验,累加。
    →_→然后再 new ArrayList(map.values())转换回 list。
    zealinux
        8
    zealinux  
    OP
       Aug 19, 2019
    @chendy

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

    有点不想再写 getId(),getCount(),hashMap 都是简单得 put 进去的。
    A 和 B 不一定是相同结构的,K/V 数量可能不一致。
    lastpass
        9
    lastpass  
       Aug 19, 2019 via Android
    当然,如果你要稍微优雅一点,可以将 hashmap 的继承类写成匿名 /私有 内部类。
    或者按照 JAVA 最正统也最优雅但也最费事儿的写法。是自己去实现一个 list 接口。自己现实 addAll 方法。实现取并集。→_→
    zealinux
        11
    zealinux  
    OP
       Aug 20, 2019
    @lqw3030 不行,addAll 只会无脑拼接。
    gonethen
        12
    gonethen  
       Aug 20, 2019
    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  
       Aug 20, 2019
    groupby 之后再 sum
    Raymon111111
        14
    Raymon111111  
       Aug 20, 2019
    就是放进 map 然后转回来
    zifangsky
        15
    zifangsky  
       Aug 20, 2019
    给你写了一份测试代码,我试了下没毛病。
    zhady009
        16
    zhady009  
       Aug 20, 2019
    Rwing
        17
    Rwing  
       Aug 20, 2019
    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  
       Aug 20, 2019
    @lhx2008 那 Object 里的 count 怎么取出呢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2877 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 314ms · UTC 10:01 · PVG 18:01 · LAX 03:01 · JFK 06:01
    ♥ Do have faith in what you're doing.