zealinux
V2EX  ›  Java

Java 如何合并 List?

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

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

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