Treemap methods

The TreeMap class has various methods that are used to operate the data of TreeMap. We have already discussed how to create a TreeMap in java. Here we will see how to put, get, iterate, and remove the element from TreeMap.

Treemap methods

Here is the table content of the article will we will cover this topic.
How to add element in TreeMap by use of TreeMap put() method
1. put(K key, V value) method
2. putAll(Map map) method
How to check given key exists by treemap containsKey() method
3. containsKey(Object key) method
How to check given value exists by treemap containsValue() method
4. containsValue(Object value) method

How to replace in Treemap by use of treemap replace() method
5. replace(K key, V value) method
6. replace(K key, V oldValue, V newValue) method
How to get HeadMap and TailMap from TreeMap?
7. headMap(K toKey) method
8. headMap(K toKey, boolean inclusive) method
9. tailMap(K fromKey) method
10. tailMap(K fromKey, boolean inclusive) method

11. get(Object key) method
12. comparator() method
13. size() method
14. clear() method
15. keySet() method
16. descendingKeySet() method
17. descendingMap() method
18. isEmpty() method
19. floorEntry(K key) method
20. ceilingEntry(K key) method
21. firstEntry() method
22. lastEntry() method
23. ceilingKey(K key) method
24. floorKey(K key) method
25. firstKey() method
26. lastKey() method
27. higherKey(K key) method
28. lowerKey(K key) method

How to add element in TreeMap by use of TreeMap put() method

1. put(K key, V value) method

This method is used to set the value with the corresponding key in TreeMap. If the TreeMap already contained the key, then the old value is replaced by a new Value.

put(K key, V value):

Where K is the key with which the specified value(V) is to be associated with TreeMap.
V is the value to be associated with the specified key(K) in TreeMap.
return NULL, if there was no mapping for key. It returns the previous value if the associated key already present.

import java.util.TreeMap;

public class TreeMapExample
{
   public static void main(String args[])
   {
	   TreeMap<Integer, String> dataTreeMap = new TreeMap<Integer, String>();
		dataTreeMap.put(1, "JavaGoal");
		dataTreeMap.put(5, "Hi");
		dataTreeMap.put(2, "Learning");
		dataTreeMap.put(4, "Hello");
		dataTreeMap.put(3, "Website");
		
		// Adding element again with same key, So it replace the old value
		System.out.println("Previous value = "+ dataTreeMap.put(1, "JavaGoal.com"));
		
		System.out.println("All objects from TreeMap: "+ dataTreeMap);
   }
}

Output: Previous value = JavaGoal
All objects from TreeMap: {1=JavaGoal.com, 2=Learning, 3=Website, 4=Hello, 5=Hi}

2. putAll(Map map) method

This method is used to copy all elements of the specified map in current TreeMap. It copies all the mapping(Key and value pairs) of the specified Map to the current TreeMap

putAll(Map map)

Where the map is a specified Map which you want to copy.
return Void, it doesn’t return anything.

import java.util.TreeMap;

public class TreeMapExample
{
   public static void main(String args[])
   {
	   TreeMap<Integer, String> dataTreeMap = new TreeMap<Integer, String>();
	   dataTreeMap.put(1, "JavaGoal");
	   dataTreeMap.put(5, "Hi");
	   dataTreeMap.put(2, "Learning");
	   dataTreeMap.put(4, "Hello");
	   dataTreeMap.put(3, "Website");
		
	   TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>(dataTreeMap);
	   System.out.println("All objects from TreeMap: "+ treeMap);
   }
}

Output: All objects from TreeMap: {1=JavaGoal, 2=Learning, 3=Website, 4=Hello, 5=Hi}

How to check given key exists by treemap containsKey() method

3. containsKey(Object key) method

This method is used to check whether the specified key exists in TreeMap or not. It returns type is boolean.
If the given key exists in TreeMap, then it returns true otherwise false.

containsKey(Object key)

Where, Key is the key with which the specified value(V) is to be associated in TreeMap.
return type:  Its return type is boolean. It can return either true or false.

import java.util.TreeMap;

public class TreeMapExample
{
   public static void main(String args[])
   {
	   TreeMap<Integer, String> dataTreeMap = new TreeMap<Integer, String>();
	   dataTreeMap.put(1, "JavaGoal");
	   dataTreeMap.put(5, "Hi");
	   dataTreeMap.put(2, "Learning");
	   dataTreeMap.put(4, "Hello");
	   dataTreeMap.put(3, "Website");
	   
	   System.out.println("Is key exists: "+dataTreeMap.containsKey(3));
	   System.out.println("Is key exists: "+dataTreeMap.containsKey(6));
   }
}

Output: Is key exists: true
Is key exists: false

How to check given value exists by treemap containsValue() method

4. containsValue(Object value) method

This method is used to check whether the specified Value exists in TreeMap or not. It returns type is boolean. If the given value exists in TreeMap, then it returns true otherwise false.

containsValue(Object value)

Where Value is the value that you want to check in TreeMap.
return type:  Its return type is boolean. It can return either true or false.

import java.util.TreeMap;

public class TreeMapExample
{
   public static void main(String args[])
   {
	   TreeMap<Integer, String> dataTreeMap = new TreeMap<Integer, String>();
	   dataTreeMap.put(1, "JavaGoal");
	   dataTreeMap.put(5, "Hi");
	   dataTreeMap.put(2, "Learning");
	   dataTreeMap.put(4, "Hello");
	   dataTreeMap.put(3, "Website");
	   
	   System.out.println("Is Value exists: "+dataTreeMap.containsValue("Hello"));
	   System.out.println("Is Value exists: "+dataTreeMap.containsValue("Bye"));
   }
}

Output: Is Value exists: true
Is Value exists: false

How to replace in Treemap by use of treemap replace() method

5. replace(K key, V value) method

This method is used to replace the entry for the specified key only if it is currently mapped to some value.

V replace(K key, V value)

Where, Key is the key with which the specified value(V) is to be associated in TreeMap.
Value is the value to be associated with the specified key(K) in TreeMap.

import java.util.TreeMap;

public class TreeMapExample
{
   public static void main(String args[])
   {
	   TreeMap<Integer, String> dataTreeMap = new TreeMap<Integer, String>();
	   dataTreeMap.put(1, "JavaGoal");
	   dataTreeMap.put(5, "Hi");
	   dataTreeMap.put(2, "Learning");
	   dataTreeMap.put(4, "Hello");
	   dataTreeMap.put(3, "Website");
	   
	   dataTreeMap.replace(2, "Learning Website");
	   System.out.println("From key 2: "+ dataTreeMap.get(2));
   }
}

Output: From key 2: Learning Website

6. replace(K key, V oldValue, V newValue) method

This method is used to replace the entry for the specified key only if it is currently mapped to some value. If oldValue doesn’t match with the associated key then it does not replace the value. Its return type is boolean.

replace(K key, V oldValue, V newValue)

Where, Key is the key with which the specified value(V) is to be associated in TreeMap.
oldValue is the value to be associated with the specified key(K) in TreeMap.
newValue is the value that will be associate with the specified key(K) in TreeMap.

import java.util.TreeMap;

public class TreeMapExample
{
   public static void main(String args[])
   {
	   TreeMap<Integer, String> dataTreeMap = new TreeMap<Integer, String>();
	   dataTreeMap.put(1, "JavaGoal");
	   dataTreeMap.put(5, "Hi");
	   dataTreeMap.put(2, "Learning");
	   dataTreeMap.put(4, "Hello");
	   dataTreeMap.put(3, "Website");
	   
	   dataTreeMap.replace(2, "Learning", "Learning Website");
	   System.out.println("From key 2: "+ dataTreeMap.get(2));
   }
}

Output: From key 2: Learning Website

How to get HeadMap and TailMap from TreeMap?

7. headMap(K toKey) method

It returns the key-value pairs whose keys are strictly less than the specified key toKey.
throws ClassCastException,  if specified key toKey is incompatible for TreeMap.
NullPointerException, if toKey is null and TreeMap uses natural ordering, or its comparator does not permit null keys.
It returns null if TreeMap is empty.

SortedMap<K,V> headMap(K toKey);
import java.util.TreeMap;

public class TreeMapExample
{
   public static void main(String args[])
   {
	   TreeMap<Integer, String> dataTreeMap = new TreeMap<Integer, String>();
	   dataTreeMap.put(1, "JavaGoal");
	   dataTreeMap.put(5, "Hi");
	   dataTreeMap.put(2, "Learning");
	   dataTreeMap.put(4, "Hello");
	   dataTreeMap.put(3, "Website");
	   
	   System.out.println("HeadMap from TreeMap: "+ dataTreeMap.headMap(3));
   }
}

Output: HeadMap from TreeMap: {1=JavaGoal, 2=Learning}

8. headMap(K toKey, boolean inclusive) method

It returns the key-value pairs whose keys are less than (or equal to if inclusive is true) toKey.
throws ClassCastException,  if specified key toKey is incompatible for TreeMap.
NullPointerException, if toKey is null and TreeMap uses natural ordering, or its comparator does not permit null keys. It returns null if TreeMap is empty.

Syntax:  SortedMap<K,V> headMap(K toKey, boolean inclusive); 
import java.util.TreeMap;

public class TreeMapExample
{
   public static void main(String args[])
   {
	   TreeMap<Integer, String> dataTreeMap = new TreeMap<Integer, String>();
	   dataTreeMap.put(1, "JavaGoal");
	   dataTreeMap.put(5, "Hi");
	   dataTreeMap.put(2, "Learning");
	   dataTreeMap.put(4, "Hello");
	   dataTreeMap.put(3, "Website");
	   
	   System.out.println("HeadMap from TreeMap: "+ dataTreeMap.headMap(3, true));
   }
}

Output: HeadMap from TreeMap: {1=JavaGoal, 2=Learning, 3=Website}

9. tailMap(K fromKey) method

It returns key-value pairs of NavigableMap whose keys are greater than or equal to fromKey.
throws ClassCastException,  if specified key toKey is incompatible for TreeMap.
NullPointerException, if toKey is null and TreeMap uses natural ordering, or its comparator does not permit null keys. It returns null if TreeMap is empty.

 SortedMap<K,V> tailMap(K fromKey);
import java.util.TreeMap;

public class TreeMapExample
{
   public static void main(String args[])
   {
	   TreeMap<Integer, String> dataTreeMap = new TreeMap<Integer, String>();
	   dataTreeMap.put(1, "JavaGoal");
	   dataTreeMap.put(5, "Hi");
	   dataTreeMap.put(2, "Learning");
	   dataTreeMap.put(4, "Hello");
	   dataTreeMap.put(3, "Website");
	   
	   System.out.println("TailMap from TreeMap: "+ dataTreeMap.tailMap(3));
   }
}

Output: TailMap from TreeMap: {3=Website, 4=Hello, 5=Hi}

10. tailMap(K fromKey, boolean inclusive) method

It returns key-value pairs of NavigableMap whose keys are greater than (or equal to, if inclusive is true).
throws ClassCastException,  if specified key toKey is incompatible for TreeMap
NullPointerException, if toKey is null and TreeMap uses natural ordering, or its comparator does not permit null keys. It returns null if TreeMap is empty.

SortedMap<K,V> tailMap(K fromKey, boolean inclusive);
import java.util.TreeMap;

public class TreeMapExample
{
   public static void main(String args[])
   {
	   TreeMap<Integer, String> dataTreeMap = new TreeMap<Integer, String>();
	   dataTreeMap.put(1, "JavaGoal");
	   dataTreeMap.put(5, "Hi");
	   dataTreeMap.put(2, "Learning");
	   dataTreeMap.put(4, "Hello");
	   dataTreeMap.put(3, "Website");
	   
	   System.out.println("TailMap from TreeMap: "+ dataTreeMap.tailMap(3, false));
   }
}

Output: TailMap from TreeMap: {4=Hello, 5=Hi}

Some other treemap java methods

11. get(Object key) method

This method is used to get or fetch the value mapped by the specified key. It returns the value if the specified key is mapped but if TreeMap hasn’t mapped for the key then it returns NULL.

get(Object key);

Where, Key is the key with which the specified value(V) is to be associated in TreeMap.
return NULL, if there was no mapping for key otherwise it returns the value that associated with the specified key.

import java.util.TreeMap;
class ExampleOfTreeMap 
{
	public static void main(String arg[])
	{
		TreeMap<String, Integer> listOfNames = new TreeMap<String, Integer>();
		listOfNames.put("C", 8);
		listOfNames.put("A", 3);
		listOfNames.put("D", 1);
		listOfNames.put("B", 5);
		
		for(String key : listOfNames.keySet())
		{
			System.out.println(listOfNames.get(key));
		}
	}
}

Output: 3
5
8
1

12. comparator() method

It returns a Comparator that used to sort the elements of TreeMap. If, it will return null if default natural sorting order is used. It returns the object of the comparator type.
If TreeMap is not using any comparator for sorting, then it will return null.
If TreeMap, is using any comparator for sorting, then it will return comparator.

Comparator<? super K> comparator();
import java.util.Comparator;
import java.util.TreeMap;

class MyComparator implements Comparator<String> 
{ 
    public int compare(String string1, String string2) 
    { 
        return string1.compareTo(string2); 
    } 
}

class ExampleOfTreeMap 
{
	public static void main(String arg[])
	{
		TreeMap<String, Integer> listOfNames = new TreeMap<String, Integer>(new MyComparator());
		listOfNames.put("C", 8);
		listOfNames.put("A", 3);
		listOfNames.put("D", 1);
		listOfNames.put("B", 5);
		
		System.out.println("It sorts the data alpabetically");
		for(String key : listOfNames.keySet())
		{
			System.out.println(listOfNames.get(key));
		}
	}
}

Output: It sorts the data alphabetically
3
5
8
1

13. size() method

This method returns the number of elements in TreeMap. Its return type is int.

int size();
import java.util.TreeMap;

public class ExampleOfTreeMap 
{
	public static void main(String[] args) 
	{
		TreeMap<Integer, String> listOfNames = new TreeMap<Integer, String>();
		
		listOfNames.put(1, "JAVA");
		listOfNames.put(2, "GOAL");
		listOfNames.put(3, "RAVI");
		
		System.out.println("Size of list = "+listOfNames.size());
		
		
	}
}

Output: Size of list = 3

14. clear() method

This method is used to remove all the elements from the TreeMap. Its return type is void. It means this method doesn’t returns anything.

void clear();
import java.util.TreeMap;

public class ExampleOfTreeMap 
{
	public static void main(String[] args) 
	{
		TreeMap<Integer, String> listOfNames = new TreeMap<Integer, String>();
		
		listOfNames.put(1, "JAVA");
		listOfNames.put(2, "GOAL");
		listOfNames.put(3, "RAVI");
		listOfNames.put(4, "SITE");
		
		// Before clear() method
		for(String name : listOfNames.values())
		{
			System.out.println("Name from list = "+name);
		}
		
		// Removing all the element by clear() method
		listOfNames.clear();
		
		System.out.println("After Clear() method list is empty : "+ listOfNames.isEmpty());
		
	}
}

Output: Name from list = JAVA
Name from list = GOAL
Name from list = RAVI
Name from list = SITE
After Clear() method list is empty : true

15. keySet() method

This method is used to fetch the Set view of all keys. It returns all the key set. This Set worked on backed of TreeMap, if any change makes in TreeMap also reflects in this Set and vice versa. It returns type is Set.

Set<K> keySet();

Where, K represents the type keys in TreeMap.

import java.util.HashMap;

public class ExampleOfHashMap 
{
	public static void main(String[] args) 
	{
		HashMap<Integer,String> listOfNames = new HashMap<Integer, String>();
		
		listOfNames.put(1, "JAVA");
		listOfNames.put(2, "GOAL");
		listOfNames.put(3, "RAVI");
		
		System.out.println("Key set of Map = "+listOfNames.keySet());
		
	}
}

Output: Key set of Map = [1, 2, 3]

16. descendingKeySet() method

This method is used to fetch the Set view of all keys in reverse order. It returns all the key in form of NavigableSet. This Set worked on backed of TreeMap, if any change makes in TreeMap also reflects in this Set and vice versa. It returns type is Set.

NavigableSet<K> descendingKeySet();

Where, K represents the type Keys in TreeMap.

import java.util.TreeMap;

public class ExampleOfTreeMap 
{
	public static void main(String[] args) 
	{
		TreeMap<Integer,String> listOfNames = new TreeMap<Integer, String>();
		listOfNames.put(1, "JAVA");
		listOfNames.put(2, "GOAL");
		listOfNames.put(3, "RAVI");
		
		System.out.println("Keys and values set of TreeMap = "+listOfNames.descendingKeySet());
		
	}
}

Output: Keys and values set of TreeMap = [3, 2, 1]

17. descendingMap() method

It returns a NavigableSet view of the Key-Value(pairs) in reverse order. This method is used to fetch the Set view of all pairs(keys-values) in reverse order. It returns all the keys and values in form of NavigableSet. This Set worked on backed of TreeMap, if any change makes in TreeMap also reflects in this Set and vice versa. It returns type is Set.

NavigableMap<K, V> descendingMap()

Where, K represents the type Keys in TreeMap and V represents the type Values in TreeMap.

import java.util.TreeMap;

public class ExampleOfTreeMap {

	public static void main(String[] args) 
	{
		TreeMap<Integer,String> listOfNames = new TreeMap<Integer, String>();
		
		listOfNames.put(1, "JAVA");
		listOfNames.put(2, "GOAL");
		listOfNames.put(3, "RAVI");
		
		System.out.println("Keys and values set of TreeMap = "+listOfNames.descendingMap());
		
	}
}

Output: Keys and values set of TreeMap = {3=RAVI, 2=GOAL, 1=JAVA}

18. isEmpty() method

This method is used to check whether the TreeSet contains an element or not. Basically, it checks the size of the elements. 
return true, if TreeSet doesn’t contain any element.
return false, if TreeSet contains any element.

boolean isEmpty();

return type:  Its return type is boolean. It can return either true or false.

import java.util.TreeMap;

public class ExampleOfTreeMap 
{
	public static void main(String[] args) 
	{
		TreeMap<Integer, String> listOfNames = new TreeMap<Integer, String>();
		listOfNames.put(1, "JAVA");
		listOfNames.put(2, "GOAL");
		listOfNames.put(3, "SITE");
		
		
		System.out.println("Is list empty = "+ listOfNames.isEmpty());
		
	}
}

Output: Is list empty = false

19. floorEntry(K key) method

It returns the pair of key-value having the closest less than or equal to the specified key. It returns null if there is no such key. If TreeMap is null, it will throw NoSuchElementException.
If you are specifying any pair(Key and value), Then TreeMap will return the pair(Key and value) whose key will be equals to the specified Key or closest less than to Specified Key.

import java.util.TreeMap;

public class TreeMapExample
{
   public static void main(String args[])
   {
	   TreeMap<Integer, String> dataTreeMap = new TreeMap<Integer, String>();
	   dataTreeMap.put(1, "JavaGoal");
	   dataTreeMap.put(5, "Hi");
	   dataTreeMap.put(2, "Learning");
	   dataTreeMap.put(4, "Hello");
	   dataTreeMap.put(3, "Website");

	   // It return the pair of closest less than specified key
	   System.out.println("Pair of Key 4: "+dataTreeMap.floorEntry(4));
	   // It return the Null because there is no greater than specified key
	   System.out.println("Pair of Key 0: "+dataTreeMap.floorEntry(0));
	   // It return the pair of itself because specified key is exists
	   System.out.println("Pair of Key 2: "+dataTreeMap.floorEntry(2));
   }
}

Output: Pair of Key 4: 4=Hello
Pair of Key 0: null
Pair of Key 2: 2=Learning

20. ceilingEntry(K key) method

It returns the pair of key-value having the closest greater than or equal to the specified key. It returns null if there is no such key. If TreeMap is null, it will throw NoSuchElementException.
If you are specifying any pair(Key and value), Then TreeMap will return the pair(Key and value) whose key will be equals to the specified Key or closest greatest to the Specified Key.

import java.util.TreeMap;

public class TreeMapExample
{
   public static void main(String args[])
   {
	   TreeMap<Integer, String> dataTreeMap = new TreeMap<Integer, String>();
	   dataTreeMap.put(1, "JavaGoal");
	   dataTreeMap.put(5, "Hi");
	   dataTreeMap.put(2, "Learning");
	   dataTreeMap.put(4, "Hello");
	   dataTreeMap.put(3, "Website");

	   // It returns the closest greater than the specified key
	   System.out.println("Pair of Key 4 = "+dataTreeMap.ceilingEntry(4));
	   // It return the Null because there is no greater than specified key
	   System.out.println("Pair of Key 10 = "+dataTreeMap.ceilingEntry(10));
	   // It return the Pair of itself because specified key is exists
	   System.out.println("Pair of Key 2 = "+dataTreeMap.ceilingEntry(2));
   }
}

Output: Pair of Key 4 = 4=Hello
Pair of Key 10 = null
Pair of Key 2 = 2=Learning

21. firstEntry() method

It returns the key-value pair having the least key(Key having lowest value) . It returns the pair that associated with least key. It returns null if TreeMap is empty.

import java.util.TreeMap;

public class TreeMapExample
{
   public static void main(String args[])
   {
	   TreeMap<Integer, String> dataTreeMap = new TreeMap<Integer, String>();
	   dataTreeMap.put(1, "JavaGoal");
	   dataTreeMap.put(5, "Hi");
	   dataTreeMap.put(2, "Learning");
	   dataTreeMap.put(4, "Hello");
	   dataTreeMap.put(3, "Website");

	   // It return the Pair of Key and value of least Key of TreeMap
	   System.out.println("Pair of least Key :"+dataTreeMap.firstEntry());
   }
}

Output: Pair of least Key :1=JavaGoal

22. lastEntry() method

It returns the key-value pair having the greatest key(Key having the highest value). It returns the pair that associated with the greatest key. It returns null if TreeMap is empty.

import java.util.TreeMap;

public class TreeMapExample
{
   public static void main(String args[])
   {
	   TreeMap<Integer, String> dataTreeMap = new TreeMap<Integer, String>();
	   dataTreeMap.put(1, "JavaGoal");
	   dataTreeMap.put(5, "Hi");
	   dataTreeMap.put(2, "Learning");
	   dataTreeMap.put(4, "Hello");
	   dataTreeMap.put(3, "Website");

	// It return the Pair of Key and value of greatest Key of TreeMap
	   System.out.println("Pair of greatest Key :"+dataTreeMap.lastEntry());
   }
}

Output: Pair of greatest Key :5=Hi

23. ceilingKey(K key) method

It returns the key which is closest greater than or equal to the specified key. It returns null if there is no such key. If TreeMap is null, it will throw NoSuchElementException.
If you are specifying any pair(Key and value), Then TreeMap will return the Key whose key will be equals to the specified Key or closest greatest to the Specified Key.

K ceilingKey(K key)
import java.util.TreeMap;

public class TreeMapExample
{
   public static void main(String args[])
   {
	   TreeMap<Integer, String> dataTreeMap = new TreeMap<Integer, String>();
	   dataTreeMap.put(1, "JavaGoal");
	   dataTreeMap.put(5, "Hi");
	   dataTreeMap.put(2, "Learning");
	   dataTreeMap.put(4, "Hello");
	   dataTreeMap.put(3, "Website");
	   
	   // It return the Key closest greater than specified key
	   System.out.println("For Key 4: "+dataTreeMap.ceilingEntry(4));
	   // It return the Null because there is no greater than specified key
	   System.out.println("For Key 10: "+dataTreeMap.ceilingEntry(10));
	   // It return the Key itself because specified key is exists
	   System.out.println("For Key 2: "+dataTreeMap.ceilingEntry(2));

   }
}

Output: For Key 4: 4=Hello
For Key 10: null
For Key 2: 2=Learning

24. floorKey(K key) method

It returns the key which is closest less than or equal to the specified key. It returns null if there is no such key. If TreeMap is null, it will throw NoSuchElementException.
If you are specifying any pair(Key and value), Then TreeMap will return the Key whose key will be equals to the specified Key or closest less than to Specified Key.

K floorKey(K key)
import java.util.TreeMap;

public class TreeMapExample
{
   public static void main(String args[])
   {
	   TreeMap<Integer, String> dataTreeMap = new TreeMap<Integer, String>();
	   dataTreeMap.put(1, "JavaGoal");
	   dataTreeMap.put(5, "Hi");
	   dataTreeMap.put(2, "Learning");
	   dataTreeMap.put(4, "Hello");
	   dataTreeMap.put(3, "Website");
	   
	   // It return the Key of closest less than specified key
	   System.out.println("For Key 4: "+dataTreeMap.floorEntry(4));
	   // It return the Null because there is no greater than specified key
	   System.out.println("For Key 0: "+dataTreeMap.floorEntry(0));
	   // It return Key itself because specified key is exists
	   System.out.println("For Key 2: "+dataTreeMap.floorEntry(2));

   }
}

Output: For Key 4: 4=Hello
For Key 0: null
For Key 2: 2=Learning

25. firstKey() method

It returns the key from TreeMap which has the least value(Key having lowest value). It returns null if TreeMap is empty.

import java.util.TreeMap;

public class TreeMapExample
{
   public static void main(String args[])
   {
	   TreeMap<Integer, String> dataTreeMap = new TreeMap<Integer, String>();
	   dataTreeMap.put(1, "JavaGoal");
	   dataTreeMap.put(5, "Hi");
	   dataTreeMap.put(2, "Learning");
	   dataTreeMap.put(4, "Hello");
	   dataTreeMap.put(3, "Website");
	   
	   // It return the Key having least Key of TreeMap
	   System.out.println("Least Key of TreeMap: "+dataTreeMap.firstKey());

   }
}

Output: Least Key of TreeMap: 1

26. lastKey() method

It returns the key from TreeMap which has the greatest value(Key having highest value). It returns null if TreeMap is empty.

import java.util.TreeMap;

public class TreeMapExample
{
   public static void main(String args[])
   {
	   TreeMap<Integer, String> dataTreeMap = new TreeMap<Integer, String>();
	   dataTreeMap.put(1, "JavaGoal");
	   dataTreeMap.put(5, "Hi");
	   dataTreeMap.put(2, "Learning");
	   dataTreeMap.put(4, "Hello");
	   dataTreeMap.put(3, "Website");
	   
	   // It return the Key having greatest Key of TreeMap
	   System.out.println("greatest Key of TreeMap: "+dataTreeMap.lastKey());

   }
}

Output: greatest Key of TreeMap: 5

27. higherKey(K key) method

It returns the key whose key strictly greater than the given key, or null if there is no such key.
throws ClassCastException,  if specified key toKey is incompatible for TreeMap.
NullPointerException, if toKey is null and TreeMap uses natural ordering, or its comparator does not permit null keys. It returns null if TreeMap is empty.

K higherKey(K key)
import java.util.TreeMap;

public class TreeMapExample
{
   public static void main(String args[])
   {
	   TreeMap<Integer, String> dataTreeMap = new TreeMap<Integer, String>();
	   dataTreeMap.put(1, "JavaGoal");
	   dataTreeMap.put(5, "Hi");
	   dataTreeMap.put(2, "Learning");
	   dataTreeMap.put(4, "Hello");
	   dataTreeMap.put(3, "Website");
	   
	   System.out.println("Key strictly greater than the given key: "+dataTreeMap.higherKey(3));

   }
}

Output: Key strictly greater than the given key: 4

28. lowerKey(K key) method

It returns the key whose key strictly less than the given key, or null if there is no such key.
throws ClassCastException,  if specified key toKey is incompatible for TreeMap.
NullPointerException, if toKey is null and TreeMap uses natural ordering, or its comparator does not permit null keys. It returns null if TreeMap is empty.

K lowerKey(K key)
import java.util.TreeMap;

public class TreeMapExample
{
   public static void main(String args[])
   {
	   TreeMap<Integer, String> dataTreeMap = new TreeMap<Integer, String>();
	   dataTreeMap.put(1, "JavaGoal");
	   dataTreeMap.put(5, "Hi");
	   dataTreeMap.put(2, "Learning");
	   dataTreeMap.put(4, "Hello");
	   dataTreeMap.put(3, "Website");
	   
	   System.out.println("Key strictly less than the given key: "+dataTreeMap.lowerKey(3));

   }
}

Output: Key strictly less than the given key: 2

Leave a Comment