This example shows how to remove the first or last entry from the TreeMap in Java. The example also shows how to remove the first or last key value from the TreeMap.
How to remove first or last entry from the TreeMap in Java?
1. How to remove first entry from the TreeMap?
There are a couple of ways using which you can remove the first key-value mapping from the TreeMap as given below.
Using the pollFirstEntry method
The pollFirstEntry
method returns and removes the first entry from the map object.
1 |
public Map.Entry<K,V> pollFirstEntry() |
It removes and returns the first key-value mapping from the map or null if the TreeMap is empty.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.TreeMap; public class TreeMapRemoveFirstLastExample { public static void main(String[] args) { TreeMap<Integer, String> tmapColors = new TreeMap<Integer, String>(); tmapColors.put(1, "Red"); tmapColors.put(2, "Green"); tmapColors.put(3, "Blue"); /* * To remove first entry from the TreeMap, use * the pollFirstEntry method */ System.out.println( "Removed: " + tmapColors.pollFirstEntry() ); System.out.println("TreeMap contains: " + tmapColors); } } |
Output
1 2 |
Removed: 1=Red TreeMap contains: {2=Green, 3=Blue} |
Using the firstKey method
We can also get the first key from the TreeMap using the firstKey
method and then remove the mapping from the TreeMap using the remove
method as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
TreeMap<Integer, String> tmapColors = new TreeMap<Integer, String>(); tmapColors.put(1, "Red"); tmapColors.put(2, "Green"); tmapColors.put(3, "Blue"); /* * Get the first key from the TreeMap using the * firstKey method and then remove the mapping */ System.out.println( "Removed: " + tmapColors.remove( tmapColors.firstKey() )); System.out.println("TreeMap contains: " + tmapColors); |
Output
1 2 |
Removed: Red TreeMap contains: {2=Green, 3=Blue} |
2. How to remove last entry from the TreeMap?
There are a couple of ways using which you can remove the last key-value mapping from the TreeMap as given below.
Using the pollLastEntry method
The pollLastEntry
method removes and returns the last entry from the TreeMap object.
1 |
public Map.Entry<K,V> pollLastEntry() |
The pollLastEntry
method returns null if the TreeMap is empty.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
TreeMap<Integer, String> tmapColors = new TreeMap<Integer, String>(); tmapColors.put(1, "Red"); tmapColors.put(2, "Green"); tmapColors.put(3, "Blue"); /* * To remove last entry from the TreeMap, use * the pollLastEntry method */ System.out.println("Removed: " + tmapColors.pollLastEntry()); System.out.println("TreeMap contains: " + tmapColors); |
Output
1 2 |
Removed: 3=Blue TreeMap contains: {1=Red, 2=Green} |
Using the lastKey method
We can also get the last key from the TreeMap using the lastKey
method and then remove the mapping from the TreeMap using the remove
method as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
TreeMap<Integer, String> tmapColors = new TreeMap<Integer, String>(); tmapColors.put(1, "Red"); tmapColors.put(2, "Green"); tmapColors.put(3, "Blue"); /* * get the last key from the TreeMap using the * lastKey method and then remove the mapping * using the remove method */ System.out.println("Removed: " + tmapColors.remove(tmapColors.lastKey())); System.out.println("TreeMap contains: " + tmapColors); |
Output
1 2 |
Removed: Blue TreeMap contains: {1=Red, 2=Green} |
This example is a part of the TreeMap in Java Tutorial.
Please let me know your views in the comments section below.
References:
Java 8 TreeMap