java對List去重並排序、如何快速地去掉兩個List中相同的部分
阿新 • • 發佈:2019-01-22
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); } }