list去除并且把值相加

package list;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; public class List2 {
public static void main(String[] args) {
List<User> us=new ArrayList<User>();
User u=new User("A",1);
us.add(u);
u=new User("C",2);
us.add(u);
u=new User("D",5);
us.add(u);
u=new User("A",4);
us.add(u);
u=new User("D",6);
us.add(u);
Map<String,Integer> map1=new HashMap<String,Integer>();
for(int i=0;i<us.size();i++){
map1.put(us.get(i).getName(),0); }
for(int i=0;i<us.size();i++){
if(map1.containsKey(us.get(i).getName())){
map1.put(us.get(i).getName(),us.get(i).getId()+map1.get(us.get(i).getName()));
} } for(Map.Entry<String, Integer> entry : map1.entrySet())
{
System.out.println(entry.getKey()+": "+entry.getValue());
}
} }

输出结果:

D: 11
A: 5
C: 2

另一种情况:文章地址https://www.yii666.com/article/764061.html

list去除并且把值相加文章来源地址https://www.yii666.com/article/764061.html网址:yii666.com文章来源地址:https://www.yii666.com/article/764061.html

要判断两个都不同的时候第才新增不能用map这种方法,只能第一次取第一个值,后面的去循环判断是否与第一个相同,不同则新增,只有两个值都相同的时候才加数字

下面这种是根据serviceType去加数字网址:yii666.com<

public List<EcrmOrderStatistics> loadStatistics(List<EOrderStat> statlist){
String prevAreaCode =""; //上一个地市
String prevProType ="" ; //上一个产品id
List<EcrmOrderStatistics> result =null;
int i=0;
if (null != statlist && statlist.size()>0) {
EcrmOrderStatistics stat=null;
if(statlist!=null&& statlist.size()>0){
prevProType = statlist.get(0).getProductName();
prevAreaCode =statlist.get(0).getAreaCode();
}
int add = 0;
int stop = 0;
int override = 0;
int changezl = 0;
int chaiji = 0;
int total=0;
for (EOrderStat row : statlist) {
if(null==stat){
stat =new EcrmOrderStatistics();
//stat.setProductId(prevProType);
stat.setAreaCode(prevAreaCode);
stat.setProductName(prevProType);
}
//当产品类型轮转
if (!prevProType.equals(row.getProductName())||!prevAreaCode.equalsIgnoreCase(row.getAreaCode())){
if(result==null){
result =new ArrayList<EcrmOrderStatistics>();
}
stat.setAddNum(add);
stat.setStopNum(stop);
stat.setOverrideNum(override);
stat.setChangezlNum(changezl);
stat.setChaijiNum(chaiji);
stat.setPerAddNum(add - chaiji);
stat.setArriveNum(total);
result.add(stat); stat =new EcrmOrderStatistics();
prevProType =row.getProductName();
prevAreaCode =row.getAreaCode();
//stat.setProductId(prevProType);
stat.setAreaCode(prevAreaCode);
stat.setProductName(prevProType);
add = 0 ;
stop = 0;
override = 0;
changezl = 0;
chaiji = 0;
total=0;
} if ("001".equals(row.getServiceType())|| "020".equals(row.getServiceType())||"007".equals(row.getServiceType()) ) {
add = add + row.getOrderNum();
} else if ("002".equals(row.getServiceType())) {
stop = stop + row.getOrderNum();
} else if ("003".equals(row.getServiceType())) {
override = override + row.getOrderNum();
} else if ("004".equals(row.getServiceType())||"022".equals(row.getServiceType())||"009".equals(row.getServiceType())) {
changezl = changezl + row.getOrderNum();
} else if ("005".equals(row.getServiceType())||"021".equals(row.getServiceType())||"008".equals(row.getServiceType())) {
chaiji = chaiji + row.getOrderNum();
}
total+=row.getOrderNum();
if((i++)==(statlist.size()-1) ){
if(result==null){
result =new ArrayList<EcrmOrderStatistics>();
}
stat.setAddNum(add);
stat.setStopNum(stop);
stat.setOverrideNum(override);
stat.setChangezlNum(changezl);
stat.setChaijiNum(chaiji);
stat.setPerAddNum(add - chaiji);
stat.setArriveNum(total);
result.add(stat);
}
} }
return result;
}

  

版权声明:本文内容来源于网络,版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。文本页已经标记具体来源原文地址,请点击原文查看来源网址,站内文章以及资源内容站长不承诺其正确性,如侵犯了您的权益,请联系站长如有侵权请联系站长,将立刻删除

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信图片_20190322181744_03.jpg

微信扫一扫打赏

请作者喝杯咖啡吧~

支付宝扫一扫领取红包,优惠每天领

二维码1

zhifubaohongbao.png

二维码2

zhifubaohongbao2.png