TreeMap Methods Tutorial With Examples in JAVA

TreeMap store values based on  key value pairs. This pair is often called as Entry. It  stores only unique elements i.e. duplicate values are not allowed and it cannot store key as null but can store null values. Mostly it is similar to HashMap Class and key difference is, it maintains an increasing order of the values based on the keys.


TreeMap Methods in JAVA

Let us discuss all the TreeMap methods one by one with Examples in Java.

1 . Object  ceilingEntry(Object key)

This method returns a Entry with the  least key greater than or equal to the given key in the argument list, or null if there is no such key, as shown in the following program

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){
 
TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

tm.put(98,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");
 
System.out.println("Ceiling entry for 99: "+ tm.ceilingEntry(99));

System.out.println("Ceiling entry for 103: "+ tm.ceilingEntry(103));

}

}

Output:

Ceiling entry for 99: 100=I

Ceiling entry for 103: 103=Java

In the output we see ceiling value of key “99” is “100” as we see 99 is not present in treemap, it automatically tool least greater than “99” which is “100”. And Ceiling value of “103” is “103”, which we see is present in the given map.

2. Object ceilingKey(Object key)

This method returns a key with the  least key greater than or equal to the given key in the argument list, or null if there is no such key, This method is same as ceilingEntry(Object key) , only difference is it returns only key, as shown in the following program

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){
 
TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

tm.put(98,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");

System.out.println("Ceiling key for 99: "+ tm.ceilingKey(99));

System.out.println("Ceiling key for 103: "+ tm.ceilingKey(103));

}

}

Output:

Ceiling key for 99: 100

Ceiling key for 103: 103

3. void clear()

This method removes all the entries from the TreeMap object as shown

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){
 
TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

tm.put(102,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");

System.out.println("TreeMap before : " + tm);

tm.clear();

System.out.println("TreeMap After clear method: " + tm);
}
}

Output:

TreeMap before : {100=I, 101=Love, 102=Core, 103=Java}

TreeMap After clear method: {}

4. Object clone()

This method returns a duplicate copy of the TreeMap object, as shown in the following program

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){

TreeMap<Integer,String> tm1 =new TreeMap<Integer,String>();

TreeMap<Integer, String> tm2 = new TreeMap<Integer, String>();

tm1.put(102,"Core");

tm1.put(100,"I");

tm1.put(103,"Java");

tm1.put(101,"Love");

//using clone method
tm2 =(TreeMap) tm1.clone();

System.out.println("Before using Clone method: "+ tm1);

System.out.println("After using Clone method: "+ tm2);
}
}

Output:

Cloning tree map

Before using Clone method: {100=I, 101=Love, 102=Core, 103=Java}

After using Clone method: {100=I, 101=Love, 102=Core, 103=Java}

5. Comparator comparator()

This method gives the comparator used in the map, which basically means in what ordering our map has data entries stored, If it returns null means given map is using natural ordering.

Let us use this in our program, and check which ordering our TreeMap has.

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){
 
TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

tm.put(98,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");

// using comparator

Comparator comparator = tm.comparator();

System.out.println("Comparator value: "+ comparator);

}

}

Output:

Comparator value: null

From output it is very clear that TreeMap uses Natural ordering.

6. boolean containsKey(Object key)

This method checks whether the given  TreeMap have the value at the specified key ,It returns true it is, or else returns false, as shown in the following program

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){

TreeMap<Integer,String> tm =new TreeMap<Integer,String>();

tm.put(102,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");

System.out.println("Checking whether there is value at key 101 "+  tm.containsKey(101));

System.out.println("Checking whether there is value at key 105 "+ tm.containsKey(105));
}

}

Output:

Checking whether there is value at key 101 true

Checking whether there is value at key 105 false

7. boolean containsValue(Object value)

This method checks whether the given  TreeMap have the value specified in the method,It returns true it is, or else returns false, as shown in the following program

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){

 

TreeMap<Integer,String> tm =new TreeMap<Integer,String>();

tm.put(102,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");

//using ContainsValue method

System.out.println("IF India exists: "+ tm.containsValue("India"));

System.out.println("IF Java exists: "+ tm.containsValue("Java"));
}
}

Output:

IF India exists: false

IF Java exists: true

8. Object  descendingKeySet()

This method returns descending order of the all the keys stored in the treemap, In the following program we are creating object of NavigableSet class and we are storing all values in this first, than we are calling descendingKeySet() method, as shown in the following program.

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){

TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

tm.put(98,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");

//putting values in navigable set

NavigableSet navigableset=tm.descendingKeySet();

 
System.out.println("Values after using descending key Set method: "+navigableset);
}

}

Output:

Values after using descending key Set method: [103, 101, 100, 98]

9. NavigableMap  descendingMap()

This method returns descending order of the all the entries stored in the treemap, In the following program we are creating object of NavigableMap class and we are storing all values in this first, than we are calling descendingMap() method, as shown in the following program.

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){

TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

tm.put(98,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");

//putting values in navigable map

NavigableMap navigablemap=tm.descendingMap() ;

System.out.println("Values after using descending map method: "+navigablemap);
}

}

Output:

Values after using descending map method: {103=Java, 101=Love, 100=I, 98=Core}

10. Set entrySet()

This method returns a Set of all the Entries in ascending key order present In the TreeMap object, as shown in the following program

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){

TreeMap<Integer,String> tm =new TreeMap<Integer,String>();

tm.put(102,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");

//using entrySet method

Set mapset=treemap.entrySet();

System.out.println("Set values: "+mapset);

}

}

Output:

Set values: [100=I, 101=Love, 102=Core, 103=Java]

11. Object firstEntry()

This method as the name suggests returns the first entry entered into the Treemap, that is the lowest entry in the treemap, as shown in the following program

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){

TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

tm.put(98,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");

System.out.println("First entry is: "+ tm.firstEntry());

}

}

Output:

First entry is: 98=Core

12. Object firstKey()

This method as the name suggests returns the first key entered into the Treemap, that is the lowest key in the treemap ,as shown in the following program

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){

TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

tm.put(102,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");

System.out.println("First key is: "+ tm.firstKey());

}

}

Output:

First key is: 100

13. Object floorEntry(Object key)

This method returns an entry with greatest key less than or equal to the key mentioned in the argument, or null if there is no such key, Let us discuss it by taking some key in a program , as shown below.

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){

TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

tm.put(98,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");


System.out.println("floor entry for '102' is: "+ tm.floorEntry(102));

System.out.println("floor entry for '99' is:"+ tm.floorEntry(99));

System.out.println("floor entry for '100' is:"+ tm.floorEntry(100));

}

}

Output:

floor entry for '102' is: 101=Love

floor entry for '99' is:98=Core

floor entry for '100' is:100=I

As we can see in output , floor entry for “102” is “101” because 102 is not present , it will automatically give “101” that is  greatest key less than or equal to given key , same is the case with key value “99”, And when we entered key “100”, as it is present it gives the same key as output.

14. Object floorKey(Object key)

This method returns the greatest key less than or equal to the given key in the argument, or null if there is no such key, this method is same as floorEntry(), only difference is it gives only key but not key-value pair, as shown in following program.

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){

TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

tm.put(98,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");

System.out.println("floor Key for '102' is: "+ tm.floorKey(102));

System.out.println("floor Key for '99' is:"+ tm.floorKey(99));

System.out.println("floor Key for '100' is:"+ tm.floorKey(100));

}

}

Output:

floor Key for '102' is: 101

floor Key for '99' is:98

floor Key for '100' is:100

15. Object get(Object key)

This methods returns the value associated with the mentioned key in the argument list, It return null if there is no mapping present, as shown in the following program

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){

TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

tm.put(102,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");

System.out.println("Value at 103 is: "+ tm.get(103));

}

}

Output:

Value at 103 is: Java

16. SortedMap headMap(Object toKey)

This method returns the part of the treemap whose key value is less than the mentioned value in the argument list, as shown in the program, we have taken a Sorted Map and used this method to store the values strictly below key 103.

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){
 
TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

SortedMap<Integer, String> tmhead = new TreeMap<Integer, String>();
 
tm.put(102,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");

//getting head map

tmhead=tm.headMap(103);

System.out.println("Value below the headMap is: "+ tmhead);
}
}

Output:

Value below the headMap is: {100=I, 101=Love, 102=Core}

17. NavigableMap  headMap(Object toKey, boolean inclusive)

This method returns a portion of map, whose key value is less than the value mentioned in the first argument, It is very much similar to the SortedMap headMap(Object toKey) method, only difference is return type and we have Boolean value , when it is true it will include the key mentioned in the program in the output, as shown in the following program.

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){

TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

NavigableMap<Integer, String> navigableMap1 = new TreeMap<Integer, String>();

NavigableMap<Integer, String> navigableMap2 = new TreeMap<Integer, String>();
 
tm.put(98,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");

// getting head map inclusive 3

navigableMap1=tm.headMap(101,true);

navigableMap2=tm.headMap(101,false);

System.out.println("Checking values of the map");

System.out.println("Values when Boolean is True "+ navigableMap1);

System.out.println("Values when Boolean is False "+ navigableMap2);
}
}

Output:

Checking values of the map

Values when Boolean is True {98=Core, 100=I, 101=Love}

Values when Boolean is False {98=Core, 100=I}

18. Object higherEntry(Object key)

This method returns the the entry with least key which is strictly greater than the given key in argument list, or null if there is no such key, as shown in following program.

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){

TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

tm.put(98,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");

System.out.println("Higher Entry then '95': "+ tm.higherEntry(95));

System.out.println("Higher Entry then '100':"+ tm.higherEntry(100));

System.out.println("Higher Entry then '103': "+ tm.higherEntry(103));
}
}

Output:

Higher Entry then '95': 98=Core

Higher Entry then '100':101=Love

Higher Entry then '103': null

As shown in output, higher key than “95”  is  “98” as we are getting in output, same with key “100”, and there is no higher key than “103” , so output is null.

19. Object higherKey(Object key)

This method returns the key with which is strictly greater than the given key in argument list, or null if there is no such key, as shown in following program.

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){

TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

tm.put(98,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");

System.out.println("Higher Key then '95': "+ tm.higherKey(95));

System.out.println("Higher Key then '100':"+ tm.higherKey(100));

System.out.println("Higher Key then '103': "+ tm.higherKey(103));
}
}

Output:

Higher Key then '95': 98

Higher Key then '100':101

Higher Key then '103': null

As shown in output, higher key than “95”  is  “98” as we are getting in output, same with key “100”, and there is no higher key than “103” , so output is null.

20. Set keySet()

This method returns a Set view of all the keys stored in the treeMap as shown in the following program

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){
 
TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

SortedMap<Integer, String> tmhead = new TreeMap<Integer, String>();

tm.put(102,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");

Set set=tm.keySet();

System.out.println("Values of key Set is: "+set);
}
}

Output:

Values of key Set is: [100, 101, 102, 103]

21. Object lastEntry()

This method as the name suggests returns the last entry or we can say highest entry present in the tree map as shown in the following program

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){

TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

tm.put(98,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");

System.out.println("Value of last entry: "+ tm.lastEntry());
}
}

Output:

Value of last entry: 103=Java

22. Object lastKey()

This method as the name suggests returns the last key or we can say highest key present in the tree map as shown in the following program

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){

TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

SortedMap<Integer, String> tmhead = new TreeMap<Integer, String>();

tm.put(102,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");

System.out.println("Value of last key is: "+ tm.lastKey());
}
}

Output:

Value of last key is: 103

23. Object  lowerEntry(Object key)

This method returns an entry with the greatest key that is strictly less than the given key in argument, or null if there is no such key, as shown in the following program.

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){

TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

tm.put(98,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");

// getting lower entry

System.out.println("Lower Entry than '103' is: "+ tm.lowerEntry(103));

System.out.println("Lower Entry than '98' is: "+ tm.lowerEntry(98));
}

}

Output:

Lower Entry than '103' is: 101=Love

Lower Entry than '98' is: null

24. Object lowerKey(Object key)

This method returns the greatest key that is strictly less than the given key in argument, or null if there is no such key, as shown in the following program.

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){

TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

tm.put(98,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");

// getting lower key

System.out.println("Lower Key than '103' is: "+ tm.lowerKey(103));

System.out.println("Lower Key than '98' is: "+ tm.lowerKey(98));
}

}

Output:

Lower Key than '103' is: 101

Lower Key than '98' is: null

25. NavigableSet navigableKeySet()

This method returns a set of all the keys in the TreeMap, as shown in the following program.

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){

TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

tm.put(98,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");

System.out.println("Value of Navigable Key Set: "+ tm.navigableKeySet());
}
}

Output:

 Value of Navigable Key Set: [98, 100, 101, 103]

26. Object pollFirstEntry()

This method returns the lowest value corresponding to the value of key, and it also removes that value from the map, if the map is empty, than it return null, as shown in the following program.

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){

TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

tm.put(98,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");

System.out.println("Value before using poll First Entry Method: "+ tm);

System.out.println("Calling Poll First Entry: "+ tm.pollFirstEntry());

System.out.println("Value After using poll First Entry Method : "+ tm);

tm.clear();

System.out.println("Calling Poll First Entry After clearing the map: "+ tm.pollFirstEntry());
}
}

Output:

Value before using poll First Entry Method: {98=Core, 100=I, 101=Love, 103=Java}

Calling Poll First Entry: 98=Core

Value After using poll First Entry Method : {100=I, 101=Love, 103=Java}

Calling Poll First Entry After clearing the map: null

27. Object pollLastEntry()

This method returns the highest value corresponding to the value of key, and it also removes that value from the map, if the map is empty, than it return null, as shown in the following program.

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){

TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

tm.put(98,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");

System.out.println("Value before using poll Last Entry Method: "+ tm);

System.out.println("Calling Poll Last Entry: "+ tm.pollLastEntry());

System.out.println("Value After using poll Last Entry Method : "+ tm);

tm.clear();

System.out.println("Calling Poll Last Entry After clearing the map: "+ tm.pollLastEntry());
}
}

Output:

Value before using poll Last Entry Method: {98=Core, 100=I, 101=Love, 103=Java}

Calling Poll Last Entry: 103=Java

Value After using poll Last Entry Method : {98=Core, 100=I, 101=Love}

Calling Poll Last Entry After clearing the map: null

28. Object put(Object key, Object value)

This method adds the specified value corresponding to the key ,If key mentioned is already present in the treemap , than it replaces it with new value that is mentioned in the argument, as shown in the followingprogram

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){

TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

tm.put(102,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");

tm.put(104,"hi");

System.out.println("Treemap :" +tm);

tm.put(103,"India") ;

System.out.println("Treemap :" +tm);
}
}

Output:

Treemap :{100=I, 101=Love, 102=Core, 103=Java, 104=hi}

Treemap :{100=I, 101=Love, 102=Core, 103=India, 104=hi}

29. void putAll(Map map)

This method copies all of the entries from the mentioned treemap to the calling treemap, It maintains the ascending order of the keys as shown in the following program

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){

TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

TreeMap<Integer, String> tm2 = new TreeMap<Integer, String>();

tm.put(102,"Core");

tm.put(100,"I");

tm.put(108,"Java");

tm.put(105,"Love");

tm2.put(101, "I");

tm2.put(103, "Love");

tm2.put(109, "India");
 
tm.putAll(tm2);

System.out.println("Values after using putAll method:" +tm);
}
}

Output:

Values after using putAll method:{100=I, 101=I, 102=Core, 103=Love, 105=Love, 108=Java, 109=India}

30. Object remove(Object key)

This method returns and removes the value for the key mentioned in the argument list from the TreeMap if it is present, as shown in the following program

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){

TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

tm.put(102,"Core");

tm.put(100,"I");

tm.put(108,"Java");

tm.put(105,"Love");
 
System.out.println("Value before: "+ tm);

// removing value at key 102

System.out.println("Removed value: "+tm.remove(102));

System.out.println("Value after : "+ tm);
}
}

Output:

Value before: {100=I, 102=Core, 105=Love, 108=Java}

Removed value: Core

Value after : {100=I, 105=Love, 108=Java}

31. int size()

This method returns the number of entries in the treeMap, as shown in the following program

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){

TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

tm.put(102,"Core");

tm.put(100,"I");

tm.put(108,"Java");

tm.put(105,"Love");

System.out.println("Size of the map: "+tm.size());
}
}

Output:

Size of the map: 4

32. NavigableMap  subMap(Object fromKey, boolean fromInclusive, Object toKey, boolean toInclusive)

This method returns group of entries of this map whose keys range from fromKey to toKey, If “fromInclusive” value is true , it will include that key value pair, same is the case with toinclusive, As shown in the following program

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){
 
TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

NavigableMap<Integer, String> nm1 = new TreeMap<Integer, String>();

NavigableMap<Integer, String> nm2 = new TreeMap<Integer, String>();

NavigableMap<Integer, String> nm3 = new TreeMap<Integer, String>();

NavigableMap<Integer, String> nm4 = new TreeMap<Integer, String>();

tm.put(98,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");

nm1=tm.subMap(98, true, 103, true);

nm2=tm.subMap(98, false, 103, false);

nm3=tm.subMap(98, true, 103, false);

nm4=tm.subMap(98, false,103, true);
 
System.out.println("values of Navigable Map 1: "+nm1);

System.out.println("values of Navigable Map 2: "+nm2);

System.out.println("values of Navigable Map 3: "+nm3);

System.out.println("values of Navigable Map 4: "+nm4);
}
}

Output:

values of Navigable Map 1: {98=Core, 100=I, 101=Love, 103=Java}

values of Navigable Map 2: {100=I, 101=Love}

values of Navigable Map 3: {98=Core, 100=I, 101=Love}

values of Navigable Map 4: {100=I, 101=Love, 103=Java}

33. SortedMap subMap(Object fromKey, Object toKey)

This method returns a group of this map whose keys range from fromKey(including its value), to toKey(excluding its value), as shown in the following program.

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){

TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

SortedMap<Integer, String> sm = new TreeMap<Integer, String>();

tm.put(98,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");

sm=tm.subMap(98,103);

System.out.println("Sub Map obtained :" +sm);
}
}

Output:

Sub Map obtained :{98=Core, 100=I, 101=Love}

34. SortedMap tailMap(Object fromKey)

This method returns a group of entries of this map whose keys are greater than or equal to fromKey, as shown in the following program.

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){

TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

SortedMap<Integer, String> sm = new TreeMap<Integer, String>();

tm.put(98,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");

sm=tm.tailMap(101);

System.out.println("Tail map values: "+sm);
}
}

Output:

Tail map values: {101=Love, 103=Java}

35. NavigableMap  tailMap(Object fromKey, boolean inclusive)

This method returns a group of entries of this map whose keys are greater than or equal to (or equal to, if inclusive is true) fromKey,  as shown in the following program.

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){

TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

SortedMap<Integer, String> sm1 = new TreeMap<Integer, String>();

SortedMap<Integer, String> sm2 = new TreeMap<Integer, String>();

tm.put(98,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");

sm1=tm.tailMap(100, true);

sm2=tm.tailMap(100, false);

System.out.println("Tail map values of First Map: "+sm1);

System.out.println("Tail map values of Second Map: "+sm2);
}
}

Output:

Tail map values of First Map: {100=I, 101=Love, 103=Java}

Tail map values of Second Map: {101=Love, 103=Java}

36. Collection values()

This method returns a collection of all the entries in the TreeMap, as shown in the following program

import java.util.*;

public class TreeMapDemo{

public static void main(String args[]){

TreeMap<Integer,String> tm=new TreeMap<Integer,String>();

tm.put(102,"Core");

tm.put(100,"I");

tm.put(103,"Java");

tm.put(101,"Love");

Collection coll=tm.values();

System.out.println("Value of the collection: "+coll);
}
}

Output:

Value of the collection: [I, Love, Core, Java]