java对List去重并排序、如何快速地去掉两个List中相同的部分

1:去重并排序

package twolist;

import java.util.Collections;  
import java.util.Comparator;  
import java.util.HashMap;  
import java.util.HashSet;  
import java.util.LinkedList;  
import java.util.List;  
import java.util.Map;  
import java.util.Set;

public class ListMapSort {
	 /** 
     * @param args 
     */  
    public static void main(String[] args) {  
        // TODO 自动生成方法存根  
        List<Map<String,Object>> listMap1 = new LinkedList<Map<String,Object>>();  
        Map<String,Object> map = new HashMap<String, Object>();  
        map.put("date", 20121010);  
        listMap1.add(map);  
        map = new HashMap<String, Object>();  
        map.put("date", 20011213);  
        listMap1.add(map);  
        listMap1.add(map);  
        map = new HashMap<String, Object>();  
        map.put("date", 20130502);  
        listMap1.add(map);  
        System.out.println("原始"+listMap1);  
          
        List<Map<String,Object>> listMap2 = new LinkedList<Map<String,Object>>();  
        Set<Map> setMap = new HashSet<Map>();  
        for(Map<String,Object> map1 : listMap1){  
            if(setMap.add(map1)){  
                listMap2.add(map1);  
            }  
        }  
        System.out.println("去重"+listMap2);  
          
        Collections.sort(listMap2, new Comparator<Map<String,Object>>(){  
            public int compare(Map<String,Object> o1,Map<String,Object> o2){  
                return o1.get("date").toString().compareTo(o2.get("date").toString());  
            }  
        });  
        System.out.println("排序:"+listMap2);  
    }  
}




2:去掉重复的后,相同的部分

package twolist;

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

public class RemoveList {

	private <K, V> void cleanListByMapKey(List<Map<K,V>> list, K toBeRemoved) {
	    List<Map<K,V>> tmpList=new ArrayList<>();
	    for(Map<K,V> m: list){
	        if(m.containsKey(toBeRemoved))
	            tmpList.add(m);
	    }
	    list.removeAll(tmpList);
	} 
	
	
	public  void testCleanListByMapKey(){
	    List<Map<String,String>> list=new ArrayList<>();
	    for(int i=0;i<10;i++){
	        Map<String, String> m=new HashMap<>();
	        m.put("key"+i, "value"+i);
	        list.add(m);
	    }
	    Map<String, String> m=new HashMap<>();
	    m.put("key100", "value100");
	    list.add(m);
	    System.out.println(list.contains(m));
	    cleanListByMapKey(list, "key100");
	    System.out.println(list.contains(m));
	}
	
	public static void main(String[] args) {
		/*RemoveList remove = new RemoveList();
		remove.testCleanListByMapKey();*/
		
		// TODO Auto-generated method stub
        Map<String, Map> msp = new HashMap<String, Map>();
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> listMap = new ArrayList<Map<String,Object>>();
        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("id", "1");
        map1.put("name", "p");
        Map<String, Object> map2 = new HashMap<String, Object>();
        map2.put("id", "3");
        map2.put("name", "h");
        Map<String, Object> map3 = new HashMap<String, Object>();
        map3.put("id", "3");
        map3.put("name", "f");
        list.add(map1);
        list.add(map3);
        list.add(map2);
        
        System.out.println("初始数据:" + list.toString());
        //把list中的数据转换成msp,去掉同一id值多余数据,保留查找到第一个id值对应的数据
        for(int i = list.size()-1 ; i>=0; i--){
            Map map = list.get(i);
            String id = (String)map.get("id");
            map.remove("id");
            msp.put(id, map);
        }
         //把msp再转换成list,就会得到根据某一字段去掉重复的数据的List<Map>
        Set<String> mspKey = msp.keySet();
        for(String key: mspKey){
            Map newMap = msp.get(key);
            newMap.put("id", key);
            listMap.add(newMap);
        }
        
        System.out.println("去掉重复数据后的数据:" + listMap.toString());
	}
}



曾经去某平安面试的时候面试官问的问题,当时回答是冒泡循环,哈哈,现在想想觉得好笑,跟冒泡循环个毛关系~~




切记,利用set的不重复,可以快速去除重复


用一个List的某个属性作为map的key,可以找到是否在list存在,这样你就可以做响应的处理

利用上面的demo,得到一个方案,解决下面的需求:

订单中商品的集合如下:



退款中的商品集合如下:




那么其它的商品就应该是XL:2件  M:2件

把其中一个list转成map,然后在用第二个list中的相关属性作为第一个map的key来判断,最终问题得以引刃而解,哈哈,牛逼!!


//【猥琐】第一步:先将原始订单中跟退款中一模一样的移除出来
						Map<String,Object> wsMap = new HashMap<String, Object>();  
						for (ApplyReturn applyReturn : groupItemList) {
							//格式itemId_color_size   qua
							wsMap.put(applyReturn.getItemId()+"_"+applyReturn.getColor()+"_"+applyReturn.getSize(), applyReturn.getQua());
						}

						List<OrderDetail> newListOrderDetails = new ArrayList<OrderDetail>();

						List<OrderDetail> listOrderDetail = order.getDetails();
						//第二步:再来遍历剩下的
						int mapQua = 0;
						for (OrderDetail orderDetail : listOrderDetail) {
							if(wsMap.get(orderDetail.getTid()+"_"+orderDetail.getKcolor()+"_"+orderDetail.getKsize())!=null){
								mapQua = Integer.parseInt(wsMap.get(orderDetail.getTid()+"_"+orderDetail.getKcolor()+"_"+orderDetail.getKsize()).toString());
								if(mapQua<orderDetail.getQua()){
									OrderDetail newOrderDetail = orderDetail;
									
									newOrderDetail.setQua(orderDetail.getQua()-mapQua);
									newListOrderDetails.add(newOrderDetail);
								}
							}else{
								newListOrderDetails.add(orderDetail);
							}
						}


已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 程序猿惹谁了 设计师:白松林 返回首页