List<Map>去重

package com.catt.webService.impl;


import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;


import org.apache.commons.lang.StringUtils;


public class AAA {


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


		Map map = new HashMap();
		map.put("1", "1");
		map.put("2", "2");
		map.put("3", "3");
		list.add(map);


		Map map1 = new HashMap();
		map1.put("1", "1");
		map1.put("2", "2");
		map1.put("3", "3");
		list.add(map1);


		Map map2 = new HashMap();
		map2.put("1", "3");
		map2.put("2", "3");
		map2.put("3", "3");
		list.add(map2);


		List li = removDuplicates(list);
		System.out.println(li);
	}


	private static List removDuplicates(List<Map> list) {
		//找到哪些位置需要移除
		List<String> has = new ArrayList<String>();
		List<Integer> needRemove = new ArrayList<Integer>();
		for (int i = 0; i < list.size(); i++) {
			Map item = list.get(i);
			Set itemSet = item.entrySet();
			List<Entry<String, String>> itemSetList = new ArrayList(itemSet);
			Collections.sort(itemSetList,new Comparator<Entry<String, String>>() {


				@Override
				public int compare(Entry<String, String> o1, Entry<String, String> o2) {
					String key = o1.getKey();
					String key2 = o2.getKey();
					return key.compareTo(key2);
				}
				
			});
			String join = StringUtils.join(itemSetList.toArray());
			if (has.contains(join)) {
				needRemove.add(i);
			}else{
				has.add(join);
			}
		}
		//移除指定位置
		List<Map> listMap = new ArrayList<Map>();
		for (Integer index : needRemove) {
			listMap.add(list.get(index.intValue()));
		}
		
		for (Map map3 : listMap) {
			list.remove(map3);
		}
		
		return list;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值