TreeMap
- Eclipse: Oxygen
- Java: 1.8
In Java TreeMap is the kind of map data, but the natural order of the keys is preserved. Therefore TreeMap implements the map interface and navigable map along with the abstract class.
In the following program, we are making use of HashMap based on lectures. The key symbolizes the serial number of the lecture and the value pair gives us the title of the lecture. By using foreach loop we will display the value.
Here we are demonstrating that TreeMap class implements a Comparable interface similar to the HashMap class but the main difference between them is that HashMap is an unordered collection while TreeMap is sorted in the ascending order of its keys.
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
class Code implements Comparable<Code>{ private String sectionNo; private String lectureNo; public Code(String sectionNo, String lectureNo) { super(); this.sectionNo = sectionNo; this.lectureNo = lectureNo; } public String getSectionNo() { return sectionNo; } public String getLectureNo() { return lectureNo; } @Override public String toString() { return "Code [sectionNo=" + sectionNo + ", lectureNo=" + lectureNo + "]"; } @Override public int compareTo(Code o) { String code1 = sectionNo.concat(lectureNo); String code2 = o.getSectionNo()+o.getLectureNo(); return code1.compareTo(code2); } } public class App { public static void main(String[] args) { Map<Code,String> lectures = new HashMap<>(); lectures.put(new Code("S01","L03"), "Generics"); lectures.put(new Code("S01","L01"), "Files under Java"); lectures.put(new Code("S02","L03"), "Network programming"); lectures.put(new Code("S01","L07"), "OOPS"); lectures.put(new Code("S01","L05"), "Methods"); lectures.put(new Code("S01","L03"), "Expressions"); for(Map.Entry<Code, String> entry: lectures.entrySet()){ System.out.println("Key: "+entry.getKey()+" Value: "+entry.getValue()); } } } |
Output
Key: Code [sectionNo=S02, lectureNo=L03] Value: Network programming
Key: Code [sectionNo=S01, lectureNo=L03] Value: Expressions
Key: Code [sectionNo=S01, lectureNo=L05] Value: Methods
Key: Code [sectionNo=S01, lectureNo=L03] Value: Generics
Key: Code [sectionNo=S01, lectureNo=L01] Value: Files under Java
Key: Code [sectionNo=S01, lectureNo=L07] Value: OOPS
In the following program, we have inserted the data in random order however when we displayed the TreeMap content we got the sorted result in the ascending order of keys.
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
class Code implements Comparable<Code>{ private String sectionNo; private String lectureNo; public Code(String sectionNo, String lectureNo) { super(); this.sectionNo = sectionNo; this.lectureNo = lectureNo; } public String getSectionNo() { return sectionNo; } public String getLectureNo() { return lectureNo; } @Override public String toString() { return "Code [sectionNo=" + sectionNo + ", lectureNo=" + lectureNo + "]"; } @Override public int compareTo(Code o) { String code1 = sectionNo.concat(lectureNo); String code2 = o.getSectionNo()+o.getLectureNo(); return code1.compareTo(code2); } } public class App { public static void main(String[] args) { Map<Code,String> lectures = new TreeMap<>(); lectures.put(new Code("S01","L03"), "Generics"); lectures.put(new Code("S01","L01"), "Files under Java"); lectures.put(new Code("S02","L03"), "Network programming"); lectures.put(new Code("S01","L07"), "OOPS"); lectures.put(new Code("S01","L05"), "Methods"); lectures.put(new Code("S01","L03"), "Expressions"); for(Map.Entry<Code, String> entry: lectures.entrySet()){ System.out.println("Key: "+entry.getKey()+" Value: "+entry.getValue()); } } } |
Output
Key: Code [sectionNo=S01, lectureNo=L01] Value: Files under Java
Key: Code [sectionNo=S01, lectureNo=L03] Value: Expressions
Key: Code [sectionNo=S01, lectureNo=L05] Value: Methods
Key: Code [sectionNo=S01, lectureNo=L07] Value: OOPS
Key: Code [sectionNo=S02, lectureNo=L03] Value: Network programming
Contributed by Poonam Tomar