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
另一种情况:
要判断两个都不同的时候第才新增不能用map这种方法,只能第一次取第一个值,后面的去循环判断是否与第一个相同,不同则新增,只有两个值都相同的时候才加数字
下面这种是根据serviceType去加数字
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;
}