java map遍歷、排序,根據value獲取key

Map 四種遍歷:java

1 Map<String,String> map = new HashMap<String, String>(); 2 map.put("one","java"); 3 map.put("two","cn"); 4 map.put("three","love");

 

第一種:取值遍歷app

1 for(String key:map.keySet()){ 2     System.out.println("key="+key+"and value=" +map.get(key)); 3 }

 

第二種:Iterator遍歷ide

1 Iterator<Map.Entry<String,String>> it = map.entrySet().iterator(); 2    while(it.hasNext()){ 3      Map.Entry<String,String> entry=it.next(); 4      System.out.println("key=" +entry.getKey() +" and value="+entry.getValue()); 5  }

 

第三種:遍歷全部的Value值工具

1 for(String v:map.values()){ 2       System.out.println("value= "+ v); 3 }

該方式取得不了key值,直接遍歷map中存放的value值。spa

 

第四種:使用entrySet遍歷code

1 for(Map.Entry<String,String > entry:map.entrySet()){ 2       System.out.println("key=" +entry.getKey() +" and value="+entry.getValue()); 3 }

 

map排序:blog

按 key 排序:排序

 1 public class MapSortDemo {  2 
 3     public static void main(String[] args) {  4 
 5         Map<String, String> map = new TreeMap<String, String>();  6 
 7         map.put("KFC", "kfc");  8         map.put("WNBA", "wnba");  9         map.put("NBA", "nba"); 10         map.put("CBA", "cba"); 11 
12         Map<String, String> resultMap = sortMapByKey(map);    //按Key進行排序
13 
14         for (Map.Entry<String, String> entry : resultMap.entrySet()) { 15             System.out.println(entry.getKey() + " " + entry.getValue()); 16  } 17  } 18     
19     /**
20  * 使用 Map按key進行排序 21  * @param map 22  * @return
23      */
24     public static Map<String, String> sortMapByKey(Map<String, String> map) { 25         if (map == null || map.isEmpty()) { 26             return null; 27  } 28 
29         Map<String, String> sortMap = new TreeMap<String, String>( 30                 new MapKeyComparator()); 31 
32  sortMap.putAll(map); 33 
34         return sortMap; 35  } 36 } 37 
38 
39 比較器類 40 
41 class MapKeyComparator implements Comparator<String>{ 42 
43  @Override 44     public int compare(String str1, String str2) { 45         
46         return str1.compareTo(str2); 47  } 48 }

 

按 value 排序:three

 1        //若是在Treemap裏面想按照value進行排序,咱們必須藉助工具類Collections.sort(List,Comparator);
 2         TreeMap<String,Object> map2 = new TreeMap<String,Object>();  3         map2.put("a","a");  4         map2.put("b","cccccc");  5         map2.put("c","bbbbb");  6         map2.put("d","eeee");  7         map2.put("e","dd");  8         ArrayList<Map.Entry<String,Object>> list = new ArrayList<Map.Entry<String,Object>>(map2.entrySet());  9         Collections.sort(list,new Comparator<Map.Entry<String,Object>>() { 10 
11  @Override 12             public int compare(Entry<String, Object> o1, Entry<String, Object> o2) { 13                 //變成按照value排列 14 // return o2.getValue().toString().compareTo(o1.getValue().toString()); 15                 //按照value的長度排序
16                 Integer o11 = o1.getValue().toString().length(); 17                 Integer o22 = o2.getValue().toString().length(); 18                 return o22.compareTo(o11); 19  } 20             
21  }); 22         
23         for(Map.Entry<String,Object> l :list){ 24             System.out.println(l.getKey()+":"+l.getValue()); 25         }

 

在 map 中根據 value 獲取 key:get

 1     //根據map的value獲取map的key 
 2     private static String getKey(Map<String,String> map,String value){  3         String key="";  4         for (Map.Entry<String, String> entry : map.entrySet()) {  5             if(value.equals(entry.getValue())){  6                 key=entry.getKey();  7  }  8  }  9         return key; 10     }

 

若要取 map 中 value 的最大值 或 與之對應的 key(整型或浮點型):可利用list

 1  //利用list取最大值
 2         List<Double> listmap = new ArrayList<Double>();  3         for(String key:mapp.keySet()){  4  listmap.add(mapp.get(key));  5  }  6         //取到最大值的value
 7         double valueMax = Collections.max(listmap);  8         //根據map的value獲取map的key 
 9         String emotionMax = ""; 10         for (Map.Entry<String, Double> entry : mapp.entrySet()) { 11             if(valueMax == entry.getValue()){ 12                 emotionMax = entry.getKey(); //取到最大值的 value 對應的 key 13  } 14         }
相關文章
相關標籤/搜索