Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

java Map to LinkedHashMap with ascending order of keys

Map<String, Integer> unSortedMap = getUnSortedMap();

//LinkedHashMap preserve the ordering of elements in which they are inserted
LinkedHashMap<String, Integer> sortedMap = new LinkedHashMap<>();

unSortedMap.entrySet()
  .stream()
  .sorted(Map.Entry.comparingByKey())
  .forEachOrdered(x -> sortedMap.put(x.getKey(), x.getValue()));
Source by howtodoinjava.com #
 
PREVIOUS NEXT
Tagged: #java #Map #LinkedHashMap #ascending #order #keys
ADD COMMENT
Topic
Name
2+5 =