The TreeMap class in Java is used to implement the Map interface, which also extends the abstract map class. The elements are sorted according to the natural sorting order or by the comparator provided during the creation time of TreeMap. TreeMap class is not threaded safe or not synchronized.
Key points about TreeMap
- It Stores elements on the basis of key and value pairs.
- This class implements Map interface including NavigableMap, SortedMap and extends AbstractMap
- It does not allow duplicate values.
- It does not allow null keys but you can insert multiple null values.
- It stores the keys in sorted order (natural order) or by a Comparator provided at map creation time.
- This class is not threaded safe, but we can make it thread-safe by using Collections.synchronizedSortedMap(new TreeMap()) for working in multi-threading environment.
Example:
SortedMap m = Collections.synchronizedSortedMap(new TreeMap(...));
Constructors in TreeMap Class
- TreeMap(): Constructs an empty treemap that will be sorted by using the natural order of its keys.
- TreeMap(Comparator comp): Constructs an empty tree-based map that will be sorted by using the Comparator comp.
- TreeMap(Map m): Initializes a treemap with the entries from m, which will be sorted by using the natural order of the keys.
- TreeMap(SortedMap sm): Initializes a treemap with the entries from sm, which will be sorted in the same order as sm.
How to Find Treemap Value ContainsValue() In Java With Example?
package com.java.Softwaretestingblog; import java.util.TreeMap; public class TreeMapValueSearch { public static void main(String[] args) { // TODO Auto-generated method stub TreeMap<String, String> hm = new TreeMap<String, String>(); //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"); } } }
Output:
{first=FIRST INSERTED, second=SECOND INSERTED, third=THIRD INSERTED} The TreeMap contains value SECOND INSERTED The TreeMap does not contain value first
How to Use Treemap Different Methods With Java Example?
package com.java.Softwaretestingblog; import java.util.TreeMap; public class Treemap_Different_Methods { public static void main(String[] args) { // TODO Auto-generated method stub TreeMap<String, String> hm = new TreeMap<String, String>(); //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 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()); } }
Output:
{first=FIRST INSERTED, second=SECOND INSERTED, third=THIRD INSERTED} Value of second: SECOND INSERTED Is TreeMap empty? false {first=FIRST INSERTED, second=SECOND INSERTED} Size of the TreeMap: 2
How to Remove Treemap Duplicate Values From Array Using Java?
package com.java.Softwaretestingblog; import java.util.Arrays; import java.util.List; import java.util.TreeSet; public class Remove_Treemap_Duplicate_Values { public static void main(String[] args) { // TODO Auto-generated method stub String[] strArr = {"one","two","three","four","four","five"}; //convert string array to list List<String> tmpList = Arrays.asList(strArr); //create a treeset with the list, which eliminates duplicates TreeSet<String> unique = new TreeSet<String>(tmpList); System.out.println(unique); } }
Output:
[five, four, one, three, two]
How to Retrieve Treemap Values Using Key In Java With Example?
package com.java.Softwaretestingblog; import java.util.Set; import java.util.TreeMap; public class Retrive_Treemap_Value_Using_Key { public static void main(String[] args) { // TODO Auto-generated method stub //www.softwaretestingblog.in TreeMap<String, String> hm = new TreeMap<String, String>(); //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<String> keys = hm.keySet(); for(String key: keys) { System.out.println("Value of "+key+" is: "+hm.get(key)); } } }
Output:
{first=FIRST INSERTED, second=SECOND INSERTED, third=THIRD INSERTED} Value of first is: FIRST INSERTED Value of second is: SECOND INSERTED Value of third is: THIRD INSERTED
How to Clear TreeMap Values In Java With Real-time Example?
package com.java.Softwaretestingblog; import java.util.TreeMap; public class ClearTreemapValues { public static void main(String[] args) { // TODO Auto-generated method stub TreeMap<String, String> hm = new TreeMap<String, String>(); //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); } }
Output:
My TreeMap content: {first=FIRST INSERTED, second=SECOND INSERTED, third=THIRD INSERTED} Clearing TreeMap: Content After clear: {}
How to Find Treemap Value containsKey() In Java With Example?
package com.java.Softwaretestingblog; import java.util.TreeMap; public class Treemap_Value_containsKey { public static void main(String[] args) { // TODO Auto-generated method stub TreeMap<String, String> hm = new TreeMap<String, String>(); //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.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"); } } }
Output:
{first=FIRST INSERTED, second=SECOND INSERTED, third=THIRD INSERTED} The TreeMap contains key first The TreeMap does not contain key fifth
Retrieve the TreeMap Values Java Program: How to Add & Retrieve TreeMap Values Using Java With Example?
package com.java.Softwaretestingblog; import java.util.Set; import java.util.TreeMap; public class ReadTreemapValues { public static void main(String[] args) { // TODO Auto-generated method stub TreeMap<String, String> hm = new TreeMap<String, String>(); //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<String> keys = hm.keySet(); for(String key: keys) { System.out.println(key); } } }
Output:
{first=FIRST INSERTED, second=SECOND INSERTED, third=THIRD INSERTED} first second third
Reference: Article
Leave a Reply