list集合对象去除重复,值相加求和

list集合对象去除重复,值相加求和

List<Proportion> proportion = new LinkedList<Proportion>();
//创建对象集合
List<T> proportionList = new LinkedList<T>();
//创建Map集合
Map<String, Integer> mapList = new HashMap<String, Integer>();
//遍历集合
for (Proportion proportion1 : proportionList) {
		//判断map集合中有没有这个name为名字的key
		//containsKey返回值为boolean类型
		if (mapList.containsKey(proportion1.getName())) {
			//根据key取出value再加上name相同的另一个参数的value
			int value = mapList.get(proportion1.getName()) + proportion1.getValue();
			存入map中,会覆盖之前key为name的那列参数
			mapList.put(proportion1.getName(), value);
			} else {
			mapList.put(proportion1.getName(), proportion1.getValue());
		}
	}
		//再将map集合转为list集合
		for (String s : mapList.keySet()) {
			Proportion ptmap = new Proportion();
			ptmap.setName(s);
			ptmap.setValue(mapList.get(s));
			proportion.add(ptmap);
		}