public static void main(String[] args) throws ParseException {
Student stu1 = new Student();
stu1.setName("a");
stu1.setAge(11);
Student stu2 = new Student();
stu2.setName("a");
stu2.setAge(22);
List<Student> list = new ArrayList<>();
list.add(stu1);
list.add(stu2);
Map<String, Integer> map = new HashMap<>();
for(Student stu:list){
String name = stu.getName();
Integer age = stu.getAge();
Integer mapAge = map.get(name);
if(null!=mapAge){
mapAge+=age;
map.put(name, mapAge);
}else{
map.put(name, age);
}
}
System.out.println(map);
}
hashSet 计算某字段相同的数据
最新推荐文章于 2022-09-21 10:18:08 发布