package com.swift;import java.util.Comparator;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Map.Entry;import java.util.Set;import java.util.TreeMap;public class Map_keySet_entrySet { public static void main(String[] args) { /* * TreeMap 集合存储自定义对象,并使用 2 中方式遍历获取 */ Maphm = new HashMap (); hm.put(new Person("lisi",18), "加拿大"); hm.put(new Person("zhangsa",17), "澳大利亚"); hm.put(new Person("zhangsa",17), "澳大利亚"); hm.put(new Person("wangwu",20), "新加坡"); hm.put(new Person("zhaoliu",19), "新西兰"); hm.put(new Person("zhaoliu",19), "新西兰"); hm.put(new Person("lisa",22), "迪拜"); //使用TreeMap无法排序比较是会出现Comparable错误 这时需要自己弄比较器 Map tree = new TreeMap (new Comparator () { @Override public int compare(Person arg0, Person arg1) { int num=arg0.getAge()-arg1.getAge(); return num==0?arg0.getName().compareTo(arg1.getName()):num; } }); tree.put(new Person("lisisi",18), "china"); //keySet方法一 Set set=hm.keySet(); for(Person per:set) { System.out.println("人员:"+per.toString()+"来自:"+hm.get(per)); } Set set1=tree.keySet(); for(Person per:set1) { System.out.println("人员:"+per.toString()+"来自:"+tree.get(per)); } //entrySet方法二 for(Map.Entry en:hm.entrySet()) { System.out.println(); System.out.println("人员:"+en.getKey().toString()+"来自:"+en.getValue()); } Map tree1 = new TreeMap (new Comparator () { @Override public int compare(Person arg0, Person arg1) { int num=arg0.getAge()-arg1.getAge(); return num==0?arg0.getName().compareTo(arg1.getName()):num; } }); tree1.put(new Person("fengqichanglin",18), "langyabang"); System.out.println(); Iterator it=tree1.entrySet().iterator(); while(it.hasNext()) { Entry entry=(Entry ) it.next(); System.out.println(entry.getKey().toString()+entry.getValue()); } }}
package sortmap_demo;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;import java.util.Map;import java.util.Map.Entry;import java.util.Set;import java.util.TreeMap;public class CollectionsSort { public static void main(String[] args) { Mapmap=new TreeMap (); map.put(1,new User("张三",28)); map.put(2,new User("李四",29)); map.put(3,new User("王五",30)); map.put(4,new User("赵六",31)); Set > set = map.entrySet(); List > list = new ArrayList >(set); Collections.sort(list,new Comparator >() { @Override public int compare(Entry o1, Entry o2) { return o2.getValue().getAge()-o1.getValue().getAge(); } }); for (Entry u : list) { System.out.println(u.getKey()+"--"+u.getValue()); }}}
不颠倒key和value的位置,用list集合api排序的方法
TreeMap根据key的Comparator对象排序方法
package sortmap_demo;import java.util.Comparator;import java.util.Map;import java.util.TreeMap;public class HashMapDemo { public static void main(String[] args) { Mapmap=new TreeMap (new Comparator () { @Override public int compare(User o1, User o2) { if(o1.getAge()>o2.getAge()){ return -1; }else if(o1.getAge()==o2.getAge()){ return 0; }else{ return 1; } } }); map.put(new User("张三",28),1); map.put(new User("李四",29),2); map.put(new User("王五",30),3); map.put(new User("赵六",31),4); for (User u : map.keySet()) { System.out.println(u+"--"+map.get(u)); }}}