Java.util - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.util TreeMap

Java.util TreeMap

shape Description

TreeMap is Red-Black tree based NavigableMap usage and is sorted by regular requesting of its keys. TreeMap class actualizes Map interface like HashMap class. The primary contrast between them is that HashMap is an unordered accumulation while TreeMap is sorted in the climbing request of its keys. TreeMap is unsynchronized gathering class which implies it is not appropriate for string safe operations until unless synchronized unequivocally.

shape Methods

The following are the various regular using methods of the class.
Methods Description
ceilingEntry(K key) to give back a key-esteem mapping connected with the minimum key more prominent than or equivalent to the given key
clear() To remove the mapping.
clone() To get the copy of an object.
containsKey(Object key) To return "genuine" if this guide contains a mapping for the predetermined key.
firstKey() To give back the main key right now in this guide.

shape Example

The following is an example. Details.java [java]package util; import java.util.TreeMap; import java.util.Set; import java.util.Iterator; import java.util.Map; public class Details { 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, "Data1"); tmap.put(23, "Data2"); tmap.put(70, "Data3"); tmap.put(4, "Data4"); tmap.put(2, "Data5"); /* 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: "); System.out.println(mentry.getValue()); } } } [/java] The iterator is used to generate the result one by one. Output: Now compile the code result will be as follows. [java] key is: 1 & Value is: Data1 key is: 2 & Value is: Data5 key is: 4 & Value is: Data4 key is: 23 & Value is: Data2 key is: 70 & Value is: Data3 [/java] The following is an example for the containsKey(Object key) method. Details.java [java]package util; import java.util.TreeMap; import java.util.Set; import java.util.Iterator; import java.util.Map; import java.util.NavigableMap; public class Details { public static void main(String args[]) { // creating tree map NavigableMap<Integer, String> treemap = new TreeMap<Integer, String>(); // populating tree map treemap.put(2, "two"); treemap.put(1, "one"); treemap.put(3, "three"); treemap.put(6, "six"); treemap.put(5, "five"); System.out.println("Checking key value 6"); System.out.println("Value for key 6 exists: "+ treemap.containsKey(6)); } } [/java] Output:Now compile the code result will be as follows. [java]Checking key value 6 Value for key 6 exists: true [/java]

Summary

shape Key Points

  • The get(Object key) strategy is utilized to give back the esteem to which the predefined key is mapped.
  • HashMap keeps up key and esteem sets and regularly meant as HashMap