This example shows how to get first or last key, value, or entry from TreeMap in Java. This example also shows how to use firstEntry, firstKey, lastEntry, and lastKey methods of TreeMap class.
How to get first or last key, value, or entry from the TreeMap in Java?
How to get the first key from the TreeMap?
The firstKey
method of the TreeMap class returns the first (i.e. the lowest) key from the map object.
1 |
public K firstKey() |
The firstKey
method throws NoSuchElementException exception if the TreeMap is empty. Make sure to check if TreeMap is not empty before calling this method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.TreeMap; public class TreeMapGetFirstLastExample { public static void main(String[] args) { TreeMap<Integer, String> treemapNumbers = new TreeMap<Integer, String>(); treemapNumbers.put(2, "Two"); treemapNumbers.put(3, "Three"); treemapNumbers.put(1, "One"); /* * To get the first key or lowest key from the TreeMap, use * the firstKey method */ if(!treemapNumbers.isEmpty()){ //this will return 1 System.out.println( treemapNumbers.firstKey() ); } } } |
Output
1 |
1 |
How to get the first entry from the TreeMap?
The firstEntry
method of the TreeMap class returns the first entry (or the entry with the lowest key) from the map object.
1 |
public Map.Entry<K,V> firstEntry() |
The firstEntry
method returns null if the map is empty.
1 2 3 4 5 6 7 8 9 10 11 |
TreeMap<Integer, String> treemapNumbers = new TreeMap<Integer, String>(); treemapNumbers.put(2, "Two"); treemapNumbers.put(3, "Three"); treemapNumbers.put(1, "One"); /* * To get the first entry, or the entry with the lowest key * from the TreeMap, use the firstEntry method */ System.out.println( treemapNumbers.firstEntry() ); |
Output
1 |
1=One |
How to get the first value from the TreeMap?
There are no direct methods to get the first value from the TreeMap. You can use either the firstKey
or the firstEntry
method to get the first value from the TreeMap.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
TreeMap<Integer, String> treemapNumbers = new TreeMap<Integer, String>(); treemapNumbers.put(2, "Two"); treemapNumbers.put(3, "Three"); treemapNumbers.put(1, "One"); /* * get the first key and then get the value mapped to it. * * Check TreeMap size first to avoid the NoSuchElementException */ if(!treemapNumbers.isEmpty()){ System.out.println( treemapNumbers.get( treemapNumbers.firstKey() ) ); } /* * OR you get the first entry and use the getValue method. * * Check for null first to avoid NullPointerException */ if(treemapNumbers.firstEntry() != null){ System.out.println( treemapNumbers.firstEntry().getValue() ); } |
Output
1 2 |
One One |
How to get the last key from the TreeMap?
The lastKey
method of the TreeMap class returns the last key (or the highest key) from the map object.
1 |
public K lastKey() |
The lastKey
method throws NoSuchElementException exception if the map is empty.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
TreeMap<Integer, String> treemapNumbers = new TreeMap<Integer, String>(); treemapNumbers.put(2, "Two"); treemapNumbers.put(3, "Three"); treemapNumbers.put(1, "One"); /* * To get the last key or highest key from the TreeMap, use * the lastKey method */ if(!treemapNumbers.isEmpty()){ //this will return 3 i.e. highest key System.out.println( treemapNumbers.lastKey() ); } |
Output
1 |
3 |
How to get the last entry from the TreeMap?
The lastEntry
method of the TreeMap class returns the last entry or the entry with the highest key in the map object.
1 |
public Map.Entry<K,V> lastEntry() |
The lastEntry
method returns null if the TreeMap is empty.
1 2 3 4 5 6 7 8 9 10 11 |
TreeMap<Integer, String> treemapNumbers = new TreeMap<Integer, String>(); treemapNumbers.put(2, "Two"); treemapNumbers.put(3, "Three"); treemapNumbers.put(1, "One"); /* * To get the last entry or entry with the highest key, use * the lastEntry method */ System.out.println( treemapNumbers.lastEntry() ); |
Output
1 |
3=Three |
How to get the last value from the TreeMap?
Same as getting the fist value, there are no direct methods to get the last value (i.e. the value mapped to the lowest or the first key) from the TreeMap object. However, we can get the last value either by getting the last key or last entry.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
TreeMap<Integer, String> treemapNumbers = new TreeMap<Integer, String>(); treemapNumbers.put(2, "Two"); treemapNumbers.put(3, "Three"); treemapNumbers.put(1, "One"); /* * Get last value using the last key */ if(!treemapNumbers.isEmpty()){ System.out.println( treemapNumbers.get( treemapNumbers.lastKey() ) ); } /* * Get the last value using the last entry */ if(treemapNumbers.lastEntry() != null){ System.out.println( treemapNumbers.lastEntry().getValue() ); } |
Output
1 2 |
Three Three |
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