How To Use Stream Sorted For Map Posted on 2022-04-22 Edited on 2024-07-03 In Sorted Views: Disqus: Symbols count in article: 566 Reading time ≈ 1 mins.How To Use Stream Sorted For Map這篇介紹How To Use Stream Sorted For Map.Solution12345678910111213Map<String, String> cache = new HashMap<>();cache.put("1","1");cache.put("3","3");cache.put("2","2"); Map<String, String> sortedMap = cache.entrySet().stream().sorted(Map.Entry.comparingByKey()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,(oldValue, newValue) -> oldValue, LinkedHashMap::new));sortedMap - Output:("1","1")("2","2")("3","3")