-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeMapExample.java
More file actions
29 lines (24 loc) · 790 Bytes
/
TreeMapExample.java
File metadata and controls
29 lines (24 loc) · 790 Bytes
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
28
29
package collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class TreeMapExample {
public static void main(String[] args) {
/* This is how to declare TreeMap */
TreeMap<Integer, String> tmap = new TreeMap<Integer, String>();
/* Adding elements to TreeMap */
tmap.put(1, "Core Java");
tmap.put(23, "Adv Java");
tmap.put(70, "Spring");
tmap.put(4, "Hibernate");
tmap.put(2, "Maven");
/* Display content using Iterator */
Set set = tmap.entrySet();
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
Map.Entry mentry = (Map.Entry) iterator.next();
System.out.print("Key is: " + mentry.getKey() + " & Value is: " + mentry.getValue() + "\n");
}
}
}