Answers for "function to sort keys in hashmap"

1

sorting hashmap by key in java 8

Map<String, String> newMapSortedByKey = sample.entrySet().stream()
                .sorted((e1,e2) -> e1.getKey().compareTo(e2.getKey()))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1,e2) -> e1, LinkedHashMap::new));
Posted by: Guest on May-20-2021
0

hashmap sort ascending

LinkedHashMap<String, Integer> sortedMap = new LinkedHashMap<>();
 
unSortedMap.entrySet()
    .stream()
    .sorted(Map.Entry.comparingByKey())
    .forEachOrdered(x -> sortedMap.put(x.getKey(), x.getValue()));
Posted by: Guest on October-31-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language