From 160241050262e1adb6f653ac40e80e6a853f8006 Mon Sep 17 00:00:00 2001 From: joao Date: Tue, 20 Oct 2020 19:23:07 -0300 Subject: [PATCH] program to sort a hashmap according to key --- Algorithms/sortHashmap.java | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Algorithms/sortHashmap.java diff --git a/Algorithms/sortHashmap.java b/Algorithms/sortHashmap.java new file mode 100644 index 0000000..64dce2b --- /dev/null +++ b/Algorithms/sortHashmap.java @@ -0,0 +1,25 @@ +import java.util.*; + +class sortmapKey { + + static Map map = new HashMap<>(); + + public static void sortbykey() + { + TreeMap sorted = new TreeMap<>(map); + + for (Map.Entry entry : sorted.entrySet()) + System.out.println("Key = " + entry.getKey() + + ", Value = " + entry.getValue()); + } + + public static void main(String args[]) + { + map.put("Jayant", 80); + map.put("Abhishek", 90); + map.put("Anushka", 80); + map.put("Amit", 75); + map.put("Danish", 40); + sortbykey(); + } +}