list 去重,list map去重

package com.cmos.ngcscontrol.service;

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


public class HandPasswordsHelper
{

/**
* @param args
*/
public static void main(String[] args)
{
Map msp = new HashMap();
List> listMap = new ArrayList>();
//上面两句是在你的基础上加的

List> list=new ArrayList>();

Map map1=new HashMap();
map1.put("orgaId",1);
map1.put("name","nihao");
map1.put("age",23);


Map map2=new HashMap();
map2.put("orgaId",1);
map2.put("name","wohao");
map2.put("age",32);



Map map3=new HashMap();
map3.put("orgaId",2);
map3.put("name","dajiahao");
map3.put("age",35);
Map map4=new HashMap();
map4.put("orgaId",2);
map4.put("name","dajiahao");
map4.put("age",35);
Map map5=new HashMap();
map5.put("orgaId",3);
map5.put("name","dajiahao");
map5.put("age",35);
Map map6=new HashMap();
map6.put("orgaId",3);
map6.put("name","dajiahao");
map6.put("age",35);


list.add(map1);
list.add(map2);
list.add(map3);
list.add(map4);
list.add(map5);
list.add(map6);

//以下内容就是在list中如何去除orgaId相同的map
System.out.println("初始化数据(原数据):" + list.toString());

//把list中的数据转换成msp,去掉同一orgaId值多余数据,保留查找到第一个orgaId值对应的数据
for(int i = list.size()-1 ; i>=0; i--){
Map map = list.get(i);
String orgaId = map.get("orgaId").toString();
map.remove("orgaId");
msp.put(orgaId, map);
}
//把msp再转换成list,就会得到根据某一字段去掉重复的数据的List
Set mspKey = msp.keySet();
for(String key: mspKey){
Map newMap = msp.get(key);
newMap.put("orgaId", key);
listMap.add(newMap);
}

System.out.println("去掉重复数据后的数据:" + listMap.toString());

for (int i = 0; i < listMap.size(); i++)
{
System.out.println("orgaId:"+listMap.get(i).get("orgaId")+"===>name:"+listMap.get(i).get("name")+"===>age:"+listMap.get(i).get("age"));
}

}
}

你可能感兴趣的:(java,web)