List<Map<String,Object>>去重,排序

来源:互联网 发布:数据分析的目的是什么 编辑:程序博客网 时间:2024/07/17 20:49

按List内Map的某一key的value值排序:

package cjw;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);}}