Here you can find the source of sortByKey(Map
Parameter | Description |
---|---|
map | a parameter |
public static <K extends Comparable<? super K>, V> HashMap<K, V> sortByKey(Map<K, V> map)
//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; } }