Java Map Sort sortByKey(final Map map, String order)

Here you can find the source of sortByKey(final Map map, String order)

Description

Sort a map by its keys according to a specified order.

License

Apache License

Parameter

Parameter Description
K Class type for the key in the map.
V Class type for the value in the map.
map The map to be sorted which can be TreeMap or HashMap .
order The String indicating the order by which the map to be sorted, either "descend" or "ascend".

Return

A sorted map by a specified order.

Declaration

public static <K extends Comparable<? super K>, V> Map<K, V> sortByKey(final Map<K, V> map, String order) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class Main {
    /**/*from ww w .ja v a2s . c o m*/
     * Sort a map by its keys according to a specified order. The input map can be 
     * any map. One can cast the returned map to {@code HashMap} but not {@code TreeMap}. 
     * 
     * @param <K> 
     *        Class type for the key in the map.
     *        
     * @param <V>
     *        Class type for the value in the map.
     *        
     * @param map
     *        The map to be sorted which can be {@code TreeMap} or {@code HashMap}.
     *        
     * @param order
     *        The {@code String} indicating the order by which the map
     *        to be sorted, either "descend" or "ascend".
     * @return
     *        A sorted map by a specified order.
     */
    public static <K extends Comparable<? super K>, V> Map<K, V> sortByKey(final Map<K, V> map, String order) {

        List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(map.entrySet());

        if (order.compareTo("ascend") == 0) {
            Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
                public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
                    return (o1.getKey()).compareTo(o2.getKey());
                }
            });
        } else if (order.compareTo("descend") == 0) {
            Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
                public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
                    return (o2.getKey()).compareTo(o1.getKey());
                }
            });
        } else {
            System.err.println("order should be either \"descend\" or \"ascend\"!");
        }

        Map<K, V> result = new LinkedHashMap<K, V>();
        for (Map.Entry<K, V> entry : list) {
            result.put(entry.getKey(), entry.getValue());
        }

        return result;
    }
}

Related

  1. sortByComparator(Map unsortMap)
  2. sortByComparator(Map unsortMap)
  3. sortByComparatorDecreasing(Map unsortMap)
  4. sortByComparatorDouble( final Map map)
  5. sortByDescendingValues(Map unsortMap)
  6. sortByKey(Map map)
  7. sortByKey(T map, final boolean descending)
  8. sortByKeys( Map map)
  9. sortByKeys(Map map)