java取List中重复的数据!

一个list中的数据,list中放的是map
比如:
金额 单位
1000 美元
2000 日元
1300 日元
1500 欧元

实现取出数据后,单位相同的相加,不相同的拼在一期,最后输出的是 1000美元3000日元1500欧元,这个循环怎么写呢

package acc.testJSON;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class AA {



public static void main(String[] args) {
List<Map<Integer,String>> list=new ArrayList<Map<Integer,String>>();

Map<Integer,String> map2=new HashMap<Integer,String>();
map2.put(1, "美元");
map2.put(2, "日元");

map2.put(3, "欧元");
map2.put(4, "日元");
map2.put(5, "人民币");
map2.put(10, "欧元");

list.add(map2);



System.out.println("--------之前---------------");
for(int i=0;i<list.size();i++){
Map<Integer,String> tempMap=list.get(i);
 for(Entry<Integer,String> e:tempMap.entrySet()){
 System.out.println("K: "+e.getKey()+" v: "+e.getValue());
 }}

 System.out.println("---------------------开始处理--------------------------");
 List<Map<String,Integer>> newList=new ArrayList<Map<String,Integer>>();
      AA aa=new AA();
      newList=aa.executMoedth(list);//如果有相同的
    

System.out.println("--------之后---------------");

for(int i=0;i<newList.size();i++){
Map<String,Integer> tempMap=newList.get(i);
 for(Entry<String,Integer> e:tempMap.entrySet()){
 System.out.println(e.getValue()+":"+e.getKey());
 }}


}


public List<Map<String,Integer>> executMoedth(List<Map<Integer,String>> list){

 System.out.println("---------------------处理中--------------------------");
 List<Map<String,Integer>> newList=new ArrayList<Map<String,Integer>>();
Map<String,Integer> newMap=new HashMap<String,Integer>();  
 
for(int i=0;i<list.size();i++){

Map<Integer,String> tempMap=list.get(i);//第i个list中的Map

 for(Entry<Integer,String> e1:tempMap.entrySet()){//map遍历 
 int tempi=0;
 for(Entry<Integer,String> e2:tempMap.entrySet()){//map遍历 

 if(e1.getValue().equals(e2.getValue())){//如果相等
 
 
 if(!e1.equals(e2))
/* tempi++;
 if(tempi>=1)*/
 {
// newMap.remove(e1.getKey());//移除第一个添加的
 int newValus=e1.getKey()+e2.getKey();//得到总钱数
 String newKey=e1.getValue();

     newMap.remove(e1.getValue());//干掉
 newMap.remove(e2.getValue());//干掉
             System.out.println("找到一个 已处理:"+newKey + newValus);
             
             newMap.put(newKey,newValus);//新集合
 
 }
 //如果新集合里没有的
 if(!newMap.containsKey(e1.getValue()))
   newMap.put(e1.getValue(),e1.getKey());//新集合

 
 }
 
 }

}
 
newList.add(newMap);//新集合 
 
}
 return newList;

}

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-09-04
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("unchecked")
public class TestList {//koukouGroup two one seven seven seven one two有不明白的进来问
public static List<Map> turn2Union(List<Map> inList){
List<Map> outList = new ArrayList<Map>();//用于返回list
Map<String,Integer> danMaps = new HashMap<String,Integer>();//用于记录单位和金额如果金额不是整数就把Integer改成double啥的
List<String> danKeys = new ArrayList<String>();//用于记录单位


for (int i = 0; i < inList.size(); i++) {
Map inMap = inList.get(i);
String oneDan = inMap.get("danwei").toString();//取出单位
if(danMaps.containsKey(oneDan)){
danMaps.put(oneDan, danMaps.get(oneDan)+Integer.valueOf(inMap.get("jine").toString()));
}else{
danMaps.put(oneDan, Integer.valueOf(inMap.get("jine").toString()));
danKeys.add(oneDan);
}
}

for (int i = 0; i < danKeys.size(); i++) {
Map map = new HashMap();
map.put("danwei",danKeys.get(i));
map.put("jine",danMaps.get(danKeys.get(i)));
outList.add(map);
}
return outList;
}

public static void main(String[] args) {
List<Map> list = new ArrayList<Map>();

Map map1 = new HashMap();
map1.put("danwei", "美元");
map1.put("jine", "1000");
list.add(map1);

Map map2 = new HashMap();
map2.put("danwei", "日元");
map2.put("jine", "2000");
list.add(map2);

Map map3 = new HashMap();
map3.put("danwei", "日元");
map3.put("jine", "1000");
list.add(map3);

Map map4 = new HashMap();
map4.put("danwei", "欧元");
map4.put("jine", "1500");
list.add(map4);

List<Map> tList = turn2Union(list);

for (int i = 0; i < tList.size(); i++) {
Map map = tList.get(i);
System.out.println(map.get("danwei")+":"+map.get("jine"));
}
}
}

本回答被提问者采纳
第2个回答  2013-07-30
netbeans eclipse
第3个回答  2013-07-29
不好说啊,你的map里面的key是什么?
相似回答