Java Map Sort sortByKey(Map map)

Here you can find the source of sortByKey(Map map)

Description

sort By Key

License

Open Source License

Parameter

Parameter Description
map a parameter

Declaration

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

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class Main {
    /**/*from w w  w . ja  v  a  2s  . co m*/
     * 
     * @param map
     * @return
     */
    public static <K extends Comparable<? super K>, V> HashMap<K, V> sortByKey(Map<K, V> map) {
        List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(map.entrySet());
        list.sort((a, b) -> {
            return (a.getKey().compareTo(b.getKey()));
        });

        HashMap<K, V> retMap = new HashMap<K, V>();
        for (Entry<K, V> entry : list) {
            retMap.put(entry.getKey(), entry.getValue());
        }
        return retMap;
    }
}

Related

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