简体   繁体   English

将部分哈希图重复项合并到Java中的新arraylist

[英]merging partial hashmap duplicates to new arraylist in java

I have Stream<Map<String, String>> where each map is like a separate record and contains two entries: 我有Stream<Map<String, String>> ,其中每个地图就像一个单独的记录,包含两个条目:

  1. Car Id ("ID", "1003") 车号(“ ID”,“ 1003”)
  2. Kilometers ("KMI", "500") 公里(“ KMI”,“ 500”)

This list might contain maps which are duplicate in a way that 2 or more maps could have the same value of car id entry. 此列表可能包含重复的地图,其方式是2张或多张地图可能具有相同的car ID输入值。 Basically this: entry1.get("ID") == entry2.get("ID"). 基本上是这样的:entry1.get(“ ID”)== entry2.get(“ ID”)。 what i want to do is to remove maps with duplicate ids but then also to merge KMI values together. 我想做的是删除具有重复ID的地图,然后将KMI值合并在一起。 this: 这个:

{"ID":"1003","KMI":"500"}, {"ID":"1003","KMI":"400"}, {"ID":"1004","KMI":"200"} {“ ID”:“ 1003”,“ KMI”:“ 500”},{“ ID”:“ 1003”,“ KMI”:“ 400”},{“ ID”:“ 1004”,“ KMI”:“ 200" }

should become this: 应该变成这个:

{"ID":"1003","KMI":"900"}, {"ID":"1004","KMI":"200"} {“ ID”:“ 1003”,“ KMI”:“ 900”},{“ ID”:“ 1004”,“ KMI”:“ 200”}

I have tried doing it with streams API alone, but I can't wrap my head around this. 我曾尝试单独使用流API来做到这一点,但我对此一无所知。 I tried modifying a similar example which was having List of objects here is what I got so far: 我尝试修改一个类似的示例,到目前为止我得到的是对象列表:

List<Map<String, String>> result = new ArrayList<>(
    queryKmAll.collect(
        Collectors.toMap(a1 -> a1.get("ID")), Function.identity(), (Map<String, String> m2, Map<String, String> m1) -> {
            m1.put("KMI", String.valueOf(Double.parseDouble(m1.get("KMI")) + Double.parseDouble(m2.get("KMI"))));

            return m1;
        })
    )
);

I have picked up from where OP left. 我从OP离开的地方接了。 I have modified your logic a bit to return what you wanted. 我对您的逻辑做了一些修改,以返回您想要的内容。 Have a look at it. 看看它。 Hope it helps 希望能帮助到你

Collectors.toMap will return a map which will have ID as key and sum of KMI for Function.identity() cases. Collectors.toMap将返回一个地图,该地图将具有ID作为键以及Function.identity()案例sum of KMI So the return would be Map<Object,Map<String,String>> . 因此,返回值为Map<Object,Map<String,String>> Because expected output is Stream<Map<String,String> , I added .values().stream() . 因为预期的输出是Stream<Map<String,String> ,所以我添加了.values().stream()

Stream<Map<String, String>> result = queryKmAll.collect(Collectors.toMap(a1 -> a1.get("ID"),
            Function.identity(), (Map<String, String> m2, Map<String, String> m1) -> {
                m1.put("KMI",
                        String.valueOf(Double.parseDouble(m1.get("KMI")) + Double.parseDouble(m2.get("KMI"))));

                return m1;
            })).values().stream();

result.forEach(System.out::println);

What you're looking for is Collectors.groupingBy : 您正在寻找的是Collectors.groupingBy

    Map<String, Long> summary = Stream
        .of(
            new HashMap<String, String>() {{
              put("ID", "1003");
              put("KMI", "500");
            }},
            new HashMap<String, String>() {{
              put("ID", "1003");
              put("KMI", "400");
            }},
            new HashMap<String, String>() {{
              put("ID", "1004");
              put("KMI", "200");
            }}
        )
        .collect(Collectors.groupingBy(
            m -> m.get("ID"),
            Collectors.summingLong(m -> Long.valueOf(m.get("KMI")))
        ));

    System.out.println(summary);

Also, you could replace the map with an class, say Summary : 同样,您可以用一个类来代替该地图,例如Summary

  public class Summary {

    public Summary(String id, Long kmi) {
      this.id = id;
      this.kmi = kmi;
    }

    private String id;
    private Long kmi;

    public String getId() {
      return id;
    }

    public Long getKmi() {
      return kmi;
    }
  }

And then use: 然后使用:

    Map<String, Long> summary = Stream
        .of(
            new Summary("1003", 500L),
            new Summary("1003", 400L),
            new Summary("1004", 200L)
        )
        .collect(Collectors.groupingBy(
            s -> s.getId(),
            Collectors.summingLong(s -> s.getKmi())
        ));

    System.out.println(summary);

Prints: {1004=200, 1003=900} 打印: {1004=200, 1003=900}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM