What is TreeMap?
TreeMap class implements the Map interface by using a tree. It provides an efficient means of storing key/value pairs in sorted order.
A TreeMap contains values based on the key. It implements the NavigableMap interface and extends AbstractMap class.
It contains only unique elements.
It cannot have null key but can have multiple null values.
It is same as HashMap instead maintains ascending order.
How to perform Basic Operations TreeMap?
The Basic Operations are:
- creating an object
- adding key-value pair objects
- getting value by passing key object
- checking whether the map has elements or not
- deleting specific entry
- size of the TreeMap
Basic Operations
import java.util.TreeMap; public class MyBasicOperations { public static void main(String a[]){ TreeMapTry it Yourselfhm = new TreeMap (); hm.put("first", "FIRST INSERTED"); hm.put("second", "SECOND INSERTED"); hm.put("third","THIRD INSERTED"); System.out.println(hm); System.out.println("Value of second: "+hm.get("second")); System.out.println("Is TreeMap empty? "+hm.isEmpty()); hm.remove("third"); System.out.println(hm); System.out.println("Size of the TreeMap: "+hm.size()); } }
How to iterate through TreeMap?
First we can get all keys by calling keySet() method, which returns list of keys as Set object. By going through each element in the set, we can get corresponding values from TreeMap.
Example
import java.util.Set; import java.util.TreeMap; public class MyTreeMapRead { public static void main(String a[]){ TreeMapTry it Yourselfhm = new TreeMap (); hm.put("first", "FIRST INSERTED"); hm.put("second", "SECOND INSERTED"); hm.put("third","THIRD INSERTED"); System.out.println(hm); Set keys = hm.keySet(); for(String key: keys){ System.out.println("Value of "+key+" is: "+hm.get(key)); } } }
How to copy Map content to another TreeMap?
By using putAll() method, we can copy all entries from another collections to TreeMap.
Example
import java.util.TreeMap; public class MyTreeMapCopy { public static void main(String a[]){ TreeMapTry it Yourselfhm = new TreeMap (); hm.put("first", "FIRST INSERTED"); hm.put("second", "SECOND INSERTED"); hm.put("third","THIRD INSERTED"); System.out.println(hm); TreeMap subMap = new TreeMap (); subMap.put("s1", "S1 VALUE"); subMap.put("s2", "S2 VALUE"); hm.putAll(subMap); System.out.println(hm); } }
How to search a key in TreeMap?
By using containsKey() method we can find out keys presence in the TreeMap.
Example
import java.util.TreeMap; public class KeySearch { public static void main(String a[]){ TreeMapTry it Yourselfhm = new TreeMap (); hm.put("first", "FIRST INSERTED"); hm.put("second", "SECOND INSERTED"); hm.put("third","THIRD INSERTED"); System.out.println(hm); if(hm.containsKey("first")){ System.out.println("The TreeMap contains key first"); } else { System.out.println("The TreeMap does not contains key first"); } if(hm.containsKey("fifth")){ System.out.println("The TreeMap contains key fifth"); } else { System.out.println("The TreeMap does not contains key fifth"); } } }
How to search a value in TreeMap?
By using containsValue() method we can find out value presence in the TreeMap.
Example
import java.util.TreeMap; public class ValueSearch { public static void main(String a[]){ TreeMapTry it Yourselfhm = new TreeMap (); //add key-value pair to TreeMap hm.put("first", "FIRST INSERTED"); hm.put("second", "SECOND INSERTED"); hm.put("third","THIRD INSERTED"); System.out.println(hm); if(hm.containsValue("SECOND INSERTED")){ System.out.println("The TreeMap contains value SECOND INSERTED"); } else { System.out.println("The TreeMap does not contains value SECOND INSERTED"); } if(hm.containsValue("first")){ System.out.println("The TreeMap contains value first"); } else { System.out.println("The TreeMap does not contains value first"); } } }
How to get all keys from TreeMap?
By using keySet() method, you can get all key entries as a Set object.
Example
import java.util.Set; import java.util.TreeMap; public class MapKeys { public static void main(String a[]){ TreeMapTry it Yourselfhm = new TreeMap (); //add key-value pair to TreeMap hm.put("first", "FIRST INSERTED"); hm.put("second", "SECOND INSERTED"); hm.put("third","THIRD INSERTED"); System.out.println(hm); Set keys = hm.keySet(); for(String key: keys){ System.out.println(key); } } }
How to get entry set from TreeMap?
Entry class provides getter methods to access key-value details. The method entrySet() provides all entries as set object.
Example
import java.util.Set; import java.util.Map.Entry; import java.util.TreeMap; public class EntrySet { public static void main(String a[]){ TreeMapTry it Yourselfhm = new TreeMap (); //add key-value pair to TreeMap hm.put("first", "FIRST INSERTED"); hm.put("second", "SECOND INSERTED"); hm.put("third","THIRD INSERTED"); System.out.println(hm); //getting value for the given key from TreeMap Set > entires = hm.entrySet(); for(Entry ent:entires){ System.out.println(ent.getKey()+" ==> "+ent.getValue()); } } }
How to delete all elements from TreeMap?
By calling clear() method, we can delete all key-value pairs from TreeMap.
Example
import java.util.TreeMap; public class MapClear { public static void main(String a[]){ TreeMapTry it Yourselfhm = new TreeMap (); //add key-value pair to TreeMap hm.put("first", "FIRST INSERTED"); hm.put("second", "SECOND INSERTED"); hm.put("third","THIRD INSERTED"); System.out.println("My TreeMap content:"); System.out.println(hm); System.out.println("Clearing TreeMap:"); hm.clear(); System.out.println("Content After clear:"); System.out.println(hm); } }
Solve this!!
import java.util.*; class Maps { public static void main(String args[]) { TreeMap obj = new TreeMap(); obj.put("A", new Integer(1)); obj.put("B", new Integer(2)); obj.put("C", new Integer(3)); System.out.println(obj.entrySet()); } }
obj.entrySet() method is used to obtain a set that contains the entries in the map [entry1,entry2....entryn].
import java.util.*; class Maps { public static void main(String args[]) { TreeMap obj = new TreeMap(); obj.put("B", new Integer(2)); obj.put(null, null); obj.put("C", new Integer(3)); System.out.println(obj.entrySet()); } }
TreeMap doesn't allow null keys.
import java.util.*; class Maps { public static void main(String args[]) { TreeMap obj = new TreeMap(); obj.put("B", null); obj.put("C", null); System.out.println(obj); } }
obj is used to obtain a set that contains the entries in the map. {entry1,entry2...entryn}.
- Collection
- ArrayList
- LinkedList
- Vector
- HashSet
- Conditional Statements
- Classes and Objects
- Inheritance
- Packages
- Exception Handling
- Threads
- TreeSet
- LinkedHashSet
- HashMap
- TreeMap
- LinkedHashMap
- HashTable
- Iterator and ListIterator
- String
- Scanner
- Array Small Programs
- Introduction
- Variables
- DataTypes
- Operators
- Conditional Statements
- Loop
- Arrays
- Object Oriented Programming
- Classes and Objects
- Access Modifiers
- Methods
- Abstraction
- Constructor
- Packages
- StringBuffer
- Inheritance
- StringBuilder
- Encapsulation
- Java IO
- Polymorphism
- Interface
- Types Of Variables
- Static Keyword
- Final Keyword
- Super Keyword
- This Keyword
- Exception Handling