仅当不同键下的值匹配时,列表中某些哈希映射值的总和

我有一个数组列表,里面装满了数千个哈希图,有四个键和值映射到它们,如下所示:


HashMap<String, Object> map = new HashMap<>();

map.put("ID", 1);

map.put("NAME", name);

map.put("WORK_TIME", workTime);

map.put("ACCOUNT", name);

现在有一个数组列表的那些哈希地图,我想总结一下具有相同ID,名称和帐户的人的工作时间,例如:


HashMap<String, Object> map1 = new HashMap<>();

map1.put("ID", 1);

map1.put("NAME", "Edward");

map1.put("WORK_TIME", 20);

map1.put("ACCOUNT", null);


HashMap<String, Object> map2 = new HashMap<>();

map2.put("ID", 1);

map2.put("NAME", "Krzych");

map2.put("WORK_TIME", 6);

map2.put("ACCOUNT", 123);


HashMap<String, Object> map3 = new HashMap<>();

map3.put("ID", 1);

map3.put("NAME", "Edward");

map3.put("WORK_TIME", 13.5);

map3.put("ACCOUNT", null);


HashMap<String, Object> map4 = new HashMap<>();

map4.put("ID", 2);

map4.put("NAME", "Grzesiek");

map4.put("WORK_TIME", 50);

map4.put("ACCOUNT", null);


HashMap<String, Object> map5 = new HashMap<>();

map5.put("ID", 2);

map5.put("NAME", "Edward");

map5.put("WORK_TIME", 12);

map5.put("ACCOUNT", 123);


[..]


ArrayList<HashMap<String,Object>> arrList = new ArrayList<>();

arrList.put..

因此,我应该得到一个包含4个哈希地图的数组列表,其中只有两个具有相同ID和帐户的爱德华哈希地图被合并成一个,工作时间为33.5。


我唯一想到的是迭代所有映射,比较这三个值,然后成功地替换存储在第二个数组列表中的哈希图中的工作时间值,我正在Java 8中工作,我想使用流来完成这一点,这可能吗?还是您看到了更好的解决方案?


SMILET
浏览 65回答 1
1回答

拉丁的传说

这个λ:Map<String,&nbsp;List<HashMap<String,&nbsp;Object>>>&nbsp;xxx&nbsp;=&nbsp;arrList.stream().collect(Collectors.groupingBy(x&nbsp;->&nbsp;((int)&nbsp;x.get("ID")&nbsp;+&nbsp;"")&nbsp;+&nbsp;x.get("NAME")));给出的结果如下:但是,如上所述 - 您应该使用(如果可能)对象而不是数组。对于对象,lambda 将如下所示:Map<String,&nbsp;List<Person>>&nbsp;xxx&nbsp;=&nbsp;arrList.stream().collect(Collectors.groupingBy(x&nbsp;->&nbsp;x.getName()&nbsp;+&nbsp;x.&nbsp;getId()));但是由于您有 Person 类,因此您可以覆盖等于以满足您的需求,并且可以在一个简单的 lambda 中映射和求和工作时间:Map<Person,&nbsp;Double>&nbsp;result&nbsp;=&nbsp;arrList.stream().collect(Collectors.groupingBy(x&nbsp;->&nbsp;x,&nbsp;Collectors.summingDouble(Person::getWorkTime)));这就是结果 - 将人员地图作为键,将工作时间摘要作为值。人员类的代码:public class Person {&nbsp; &nbsp; &nbsp; &nbsp; String name;&nbsp; &nbsp; &nbsp; &nbsp; int id;&nbsp; &nbsp; &nbsp; &nbsp; double workTime;&nbsp; &nbsp; &nbsp; &nbsp; public Person(String name, int id, double workTime) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.workTime = workTime;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public boolean equals(Object o) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (this == o) return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (o == null || getClass() != o.getClass()) return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Person person = (Person) o;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return id == person.id &&&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Objects.equals(name, person.name);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public int hashCode() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Objects.hash(name, id);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }示例代码:Person person1 = new Person("Edward", 1, 20);Person person2 = new Person("Krzych", 1, 6);Person person3 = new Person("Edward", 1, 13.5);Person person4 = new Person("Grzesiek", 2, 50);Person person5 = new Person("Edward", 2, 12);ArrayList<Person> arrList = new ArrayList<>();arrList.add(person1);arrList.add(person2);arrList.add(person3);arrList.add(person4);arrList.add(person5);Map<Person, Double> result = arrList.stream().collect(Collectors.groupingBy(x -> x, Collectors.summingDouble(Person::getWorkTime)));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java