Java TreeMap higherKey() Method
Example
Find the key that comes just after a given key:
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
TreeMap<String, String> capitalCities = new TreeMap<String, String>();
capitalCities.put("England", "London");
capitalCities.put("Austria", "Wien");
capitalCities.put("Norway", "Oslo");
capitalCities.put("USA", "Washington DC");
System.out.println(capitalCities.higherKey("Norway"));
System.out.println(capitalCities.higherKey("USA"));
}
}
Definition and Usage
The higherKey() method returns the least (lowest) key in the map that is greater than the specified key. For text keys, "greater than" means "comes after in alphabetical order".
Unlike ceilingKey(), it never returns the specified key itself. If there is no key after the specified key, the method returns null.
Tip: Use the lowerKey() method to find the key that comes just before a given key.
Syntax
public K higherKey(K key)
K refers to the data type of the keys in the map.
Parameter Values
| Parameter | Description |
|---|---|
| key | Required. The key to compare with. It does not have to be in the map. |
Technical Details
| Returns: | The least key that is greater than key, or null if there is no such key. |
|---|
Related Pages
❮ TreeMap Methods