利用SortedMap对HashMap进行排序

import java.util.HashMap;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

public class SortTest {

	public static void main(String[] args) {
		 HashMap<String,String> map=new HashMap<String, String>();
		 map.put("a","a");
		 map.put("s", "s");
		 map.put("o", "o");
		 map.put("z", "z");
		 map.put("b", "b");
		 map.put("t", "t");
		/* for (Map.Entry<String,String> entry: map.entrySet()) {
		     System.out.println("排序之前:"+entry.getKey()+" 值"+entry.getValue());

		 }*/

		 Set<String> set = map.keySet();
		 for(String s:set){
		     map.get(s);
		     System.out.println("排序之前:"+s+" 值"+map.get(s));
		 }
		 System.out.println("=========================SortedMap进行处理=============================");
		 SortedMap<String,String> sort=new TreeMap<String,String>(map);
		/* Set<Map.Entry<String,String>> entry1=sort.entrySet();
		 Iterator<Map.Entry<String,String>> it=entry1.iterator();
		 while(it.hasNext())
		 {
		     Map.Entry<String,String> entry=it.next();
		     System.out.println("排序之后:"+entry.getKey()+" 值"+entry.getValue());
		 }*/
		 Set<String> sortSet = sort.keySet();
		 for (String s :sortSet){
		     System.out.println("排序之后:"+s+" 值"+sort.get(s));
		 }
		
	}

}

  

posted @ 2018-08-20 14:47  86727515  阅读(669)  评论(0编辑  收藏  举报