JAVA map按照key,value比較

import java.util.*;

public class MapSortDemo {

   public static void main(String[] args) {

       Map<String, String> map = new HashMap<>();

       map.put("1", "4");
       map.put("3", "3");
       map.put("2", "2");
       map.put("4", "1");

       //Map<String, String> resultMap = sortMapByKey(map);    //按Key進行排序:1.treemap的性質
       
Map<String, String> resultMap = sortMapByKey2(map); //按key進行排序:2.list,自定義比較器(排序後需要LinkedHashMap保存)
      // Map<String, String> resultMap = sortMapByValue(map); //按Value進行排序:list,自定義比較器

       
for (Map.Entry<String, String> entry : resultMap.entrySet()) {
           System.out.println(entry.getKey() + " " + entry.getValue());
       }
   }

   /**
    * 使用 TreeMap的性質按key進行排序
    *
@param map
   
* @return
   
*/
   
public static Map<String, String> sortMapByKey(Map<String, String> map) {
       if (map == null || map.isEmpty()) {
           return null;
       }
       //TreeMap默認用key的自然排序,所以不用聲明比較器也可以實現key排序,比較器可以自定義排序規則,比如倒序
       //Map<String, String> sortMap = new TreeMap<String, String>();

       //TreeMap構造方法可以有比較器參數~但是比較器只能是對key進行比較
       
Map<String, String> sortMap = new TreeMap<String, String>(new MapKeyComparator());

       sortMap.putAll(map);

       return sortMap;
   }

   /**
    * 使用 list按key進行排序
    *
@param map
   
* @return
   
*/
   
public static Map<String, String> sortMapByKey2(Map<String, String> map) {
       if (map == null || map.isEmpty()) {
           return null;
       }
       List<Map.Entry<String,String>> list = new ArrayList<>(map.entrySet());
       Collections.sort(list,new MapKeyComparator2());

       Map<String, String> sortMap = new LinkedHashMap<>();
       Iterator<Map.Entry<String, String>> iterable = list.iterator();
       while (iterable.hasNext()){
           Map.Entry<String, String> tmpEntry = iterable.next();
           sortMap.put(tmpEntry.getKey(),tmpEntry.getValue());
       }

       return sortMap;
   }

   /**
    * 使用 List對Map按value進行排序
    *
@param oriMap
   
* @return
   
*/
   
public static Map<String, String> sortMapByValue(Map<String, String> oriMap) {
       if (oriMap == null || oriMap.isEmpty()) {
           return null;
       }
       //一定是LinkedHashMap,因爲LinkedHashMap保證put順序和輸出順序一致!
       
Map<String, String> sortedMap = new LinkedHashMap<>();

       //map.entry把map的<key,value>當節點裝進list,對list排序
       
List<Map.Entry<String, String>> entryList = new ArrayList<>(oriMap.entrySet());

       Collections.sort(entryList, new MapValueComparator());

       Iterator<Map.Entry<String, String>> iter = entryList.iterator();
       Map.Entry<String, String> tmpEntry = null;
       while (iter.hasNext()) {
           tmpEntry = iter.next();
           sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());
       }
       return sortedMap;
   }

}



class MapKeyComparator implements Comparator<String> {

   @Override
   
public int compare(String str1, String str2) {

       return str2.compareTo(str1);
   }
}


class MapValueComparator implements Comparator<Map.Entry<String, String>> {

   @Override
   
public int compare(Map.Entry<String, String> me1, Map.Entry<String, String> me2) {

       return me1.getValue().compareTo(me2.getValue());
   }
}

class MapKeyComparator2 implements Comparator<Map.Entry<String, String>> {

   @Override
   
public int compare(Map.Entry<String, String> me1, Map.Entry<String, String> me2) {

       return me1.getKey().compareTo(me2.getKey());
   }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章