forked from ishanjogalekar/Java-Programs
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path25.sort_hashmap.java
More file actions
Latest commit
27 lines (27 loc) · 1.02 KB
/
Copy path25.sort_hashmap.java
File metadata and controls
27 lines (27 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//Sort Hashmap based on key values using Treemap
importjava.util.*;
publicclasssort_hashmap {
publicstaticvoidmain(String[] args) {
HashMap<Integer,String> hash_map = newHashMap<>();
intn;
Stringmap_value;
intmap_key;
Scannersc = newScanner(System.in);
System.out.println("Enter size of Hash map: ");
n = sc.nextInt();
//User-input and put function to insert into hashmap
for(inti=0;i<n;i++){
System.out.println("Enter key in hash-map at "+(i+1));
map_key = sc.nextInt();
System.out.println("Enter value in hash-map at "+(i+1));
map_value = sc.next();
hash_map.put(map_key,map_value);
}
System.out.println("Sorting Hash map based on key ");
TreeMap<Integer, String> tree_map = newTreeMap<>(hash_map);
for (Integerinteger : tree_map.keySet()) {
intkey = integer;
System.out.println(key + ": " + hash_map.get(key));
}
}
}