NavigableMap Interface in Java - Walking Techie

Blog about Java programming, Design Pattern, and Data Structure.

Wednesday, February 15, 2017

NavigableMap Interface in Java

NavigableMap Interface

The java.util.NavigableMap interface extends java.util.SortedMap interface. It has a few extensions to the SortedSet which makes it possible to navigate the map, which provides convenient navigation method like lowerKey, floorKey, ceilingKey and higherKey, and along with these popular navigation method. It also provides ways to create a Sub Map from existing Map in Java e.g. headMap whose keys are less than specified key, tailMap whose keys are greater than specified key and a subMap which strictly contains keys which fall between toKey and fromKey.

Here is the topic that will cover in this post.

  1. NavigableMap Interface Declaration
  2. NavigableMap Implementations
  3. NavigableMap Interface Methods
  4. Example
  5. Exceptions

NavigableMap Interface Declaration

This interface is a member of java the java Collection framework. It is available since Java 1.6. NavigableMap is a generic interface and declared as show here:

public interface NavigableMap<K,V> extends SortedMap<K,V>

Here, K specifies the type of keys, and V specifies the type of values associated with the keys.

NavigableMap Interface Hierarchy

NavigableMap Implementations

The java.util package has only one implementation of the NavigableMap interface - the java.util.TreeMap class. The java.util.concurrent package also has an implementation of this interface.

NavigableMap navigableMap = new TreeMap();

Comparator comparator = new MyComparator();
NavigableMap navigableMap1 = new TreeMap(comparator);

NavigableMap Interface Methods

Method Description
Map.Entry<K,V> lowerEntry(K key) Returns a key-value mapping associated with the greatest key strictly less than the given key, or null if there is no such key.
K lowerKey(K key) Returns the greatest key strictly less than the given key, or null if there is no such key.
Map.Entry<K,V> floorEntry(K key) Returns a key-value mapping associated with the greatest key less than or equal to the given key, or null if there is no such key.
K floorKey(K key) Returns the greatest key less than or equal to the given key, or null if there is no such key.
Map.Entry<K,V> ceilingEntry(K key) Returns a key-value mapping associated with the least key greater than or equal to the given key, or null if there is no such key.
K ceilingKey(K key) Returns the least key greater than or equal to the given key, or null if there is no such key.
Map.Entry<K,V> higherEntry(K key) Returns a key-value mapping associated with the least key strictly greater than the given key, or null if there is no such key.
K higherKey(K key) Returns the least key strictly greater than the given key, or null if there is no such key.
Map.Entry<K,V> firstEntry() Returns a key-value mapping associated with the least key in this map, or null if the map is empty.
Map.Entry<K,V> lastEntry() Returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.
Map.Entry<K,V> pollFirstEntry() Removes and returns a key-value mapping associated with the least key in this map, or null if the map is empty.
Map.Entry<K,V> pollLastEntry() Removes and returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.
NavigableMap<K,V> descendingMap() Returns a reverse order view of the mappings contained in this map. The descending map is backed by this map, so changes to the map are reflected in the descending map, and vice-versa. If either map is modified while an iteration over a collection view of either map is in progress (except through the iterator's own remove operation), the results of the iteration are undefined.
NavigableSet<K> navigableKeySet() Returns a NavigableSet view of the keys contained in this map. The set's iterator returns the keys in ascending order. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined.
NavigableSet<K> descendingKeySet() Returns a reverse order NavigableSet view of the keys contained in this map. The set's iterator returns the keys in descending order. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined.
NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) Returns a view of the portion of this map whose keys range from fromKey to toKey. If fromKey and toKey are equal, the returned map is empty unless fromInclusive and toInclusive are both true. The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa.
NavigableMap<K,V> headMap(K toKey, boolean inclusive) Returns a view of the portion of this map whose keys are less than (or equal to, if inclusive is true) toKey. The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa.
NavigableMap<K,V> tailMap(K fromKey, boolean inclusive) Returns a view of the portion of this map whose keys are greater than (or equal to, if inclusive is true) fromKey. The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa.
SortedMap<K,V> subMap(K fromKey, K toKey) Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive. (If fromKey and toKey are equal, the returned map is empty.) The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa.
SortedMap<K,V> headMap(K toKey) Returns a view of the portion of this map whose keys are strictly less than toKey. The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa
SortedMap<K,V> tailMap(K fromKey) Returns a view of the portion of this map whose keys are greater than or equal to fromKey. The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa.

Example

package com.walking.techie;

import java.util.NavigableMap;
import java.util.TreeMap;

public class NavigableMapDemo {

  public static void main(String[] args) {
    NavigableMap<Integer, String> httpStatusMap = new TreeMap<>();
    httpStatusMap.put(100, "Continue");
    httpStatusMap.put(101, "Switching Protocols");
    httpStatusMap.put(102, "Processing");
    httpStatusMap.put(403, "Forbidden");
    httpStatusMap.put(404, "Not Found");
    httpStatusMap.put(405, "Method Not Allowed");
    httpStatusMap.put(500, "Server Error");

    System.out.println("Descending Set  : " + httpStatusMap.descendingKeySet());
    System.out.println("Floor Entry  : " + httpStatusMap.floorEntry(403));
    System.out.println("First Entry  : " + httpStatusMap.firstEntry());
    System.out.println("Last Key : " + httpStatusMap.lastKey());
    System.out.println("First Key : " + httpStatusMap.firstKey());
    System.out.println("Original Map : " + httpStatusMap);
    System.out.println("Reverse Map :" + httpStatusMap.descendingMap());
  }
}

Output of above program is shown below:

Descending Set  : [500, 405, 404, 403, 102, 101, 100]
Floor Entry  : 403=Forbidden
First Entry  : 100=Continue
Last Key : 500
First Key : 100
Original Map : {100=Continue, 101=Switching Protocols, 102=Processing, 403=Forbidden, 404=Not Found, 405=Method Not Allowed, 500=Server Error}
Reverse Map :{500=Server Error, 405=Method Not Allowed, 404=Not Found, 403=Forbidden, 102=Processing, 101=Switching Protocols, 100=Continue}

Exceptions

NavigableMap interface has many methods, and several methods throw below exceptions.

Exception Description
ClassCastException throws when an object is incompatible with the keys in the map.
NullPointerException thrown if an attempt is made to use a null object and null is not allowed in the map.
IllegalArgumentException thrown if an invalid argument is used.

No comments :

Post a Comment