Java Collections - NavigableMap in Java - Java @ Desk

Friday, April 11, 2014

Java Collections - NavigableMap in Java

Java Collections - NavigableMap in Java

java.util.NavigableMap is an interface in java which is being implemented by java.util.TreeMap

TreeMap is a sorted map using the keys of the collection.

So if you are using a TreeMap in your application, following value can be easily achieved with a method calls:
1) Last Key
2) First Key
3) Descending Map
4) Descending Key Set
5) Head Map
6) Tail Map
7) Sub Map
8) Ceiling Key
9) Floor Key
10) Higher Key
11) Lower Key
12) Ceiling Entry, Floor Entry, Higher Entry, Lower Entry
13) Poll First Entry
14) Poll Last Entry

1) Last Key - This returns the last key from the Navigable Map

2) First Key - This returns the first key from the Navigable Map

3) Descending Map - This returns the map object where the keys are in descending order

4) Descending Key Set - This returns the NavigableSet object of keys in descending order

5) Head Map - This returns the Map object that will include all the keys strictly less than the key passed to the method. If true is passed along with the key in a method, the key will also be included in the map returned

6) Tail Map - This returns the Map object that will include current key plus all the keys greater than the key passed to the method. If false is passed along with the key in a method, the passed key will not be included in the map returned

7) Sub Map - This returns a map object in the range. The range includes the keys from fromKey till the key less the toKey passed in the method call. If method with boolean parameter is used and if both the boolean passed are false, then fromKey and toKey will be excluded from the map returned

8) Ceiling Key - This returns the key from the map which is either equal to or greater than the key passed to the method

9) Floor Key - This returns the key from the map which is either equal to or less than the key passed to the method

10) Higher Key - This returns the key from the map which is greater than the key passed to the method

11) Lower Key - This returns the key from the map which is less than the key passed to the method

12) Ceiling Entry, Floor Entry, Higher Entry, Lower Entry - This works the same way as above 4 points. The only difference is, it returns the Map.Entry object instead of a key

13) Poll First Entry - This returns and removes the Map.Entry object of the first entry from Navigable Map. It returns NULL if the map is empty

14) Poll Last Entry - This returns and removes the Map.Entry object of the last entry from Navigable Map. It returns NULL if the map is empty

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

public class Testing {

 public static void main(String args[]) throws InterruptedException {
  NavigableMap<String, Integer> navigableMap = new TreeMap<String, Integer>();
  navigableMap.put("Larsen", 2);
  navigableMap.put("Adani", 10);
  navigableMap.put("Reliance", 10);
  navigableMap.put("Tata Motors", 10);
  navigableMap.put("Mahindra", 10);
  navigableMap.put("Birla", 10);
  
  System.out.println("Last Key - "+navigableMap.lastKey());
  System.out.println("First Key - "+navigableMap.firstKey());
  System.out.println("Descending Key Set - " + navigableMap.descendingKeySet());
  System.out.println("Navigable Key Set - " + navigableMap.navigableKeySet());
  
  System.out.println("Descending Map - " + navigableMap.descendingMap());
  System.out.println("Head Map - " + navigableMap.headMap("Reliance"));
  System.out.println("Head Map Including Key Passed - " + navigableMap.headMap("Reliance", true));
  
  System.out.println("Tail Map Including Key Passed - " + navigableMap.tailMap("Reliance"));
  System.out.println("Tail Map Without Key Passed - " + navigableMap.tailMap("Reliance", false));
  
  System.out.println("Sub Map Including toKey - " + navigableMap.subMap("Larsen", "Tata Motors"));
  System.out.println("Sub Map without toKey and fromKey - " + navigableMap.subMap("Larsen", false, "Tata Motors", false));
  
  System.out.println("Ceiling Key - " + navigableMap.ceilingKey("Reliance"));
  System.out.println("Floor Key - " + navigableMap.floorKey("Reliance"));
  System.out.println("Higher Key - " + navigableMap.higherKey("Reliance"));
  System.out.println("Lower Key - " + navigableMap.lowerKey("Reliance"));
  
  System.out.println("Ceiling Entry - " + navigableMap.ceilingEntry("Reliance"));
  System.out.println("Floor Entry - " + navigableMap.floorEntry("Reliance"));
  System.out.println("Higher Entry - " + navigableMap.higherEntry("Reliance"));
  System.out.println("Lower Entry - " + navigableMap.lowerEntry("Reliance"));
  
  System.out.println("Poll First Entry - " + navigableMap.pollFirstEntry());
  System.out.println("Poll Last Entry - " + navigableMap.pollLastEntry());
 }
 
}



Output -

Last Key - Tata Motors
First Key - Adani
Descending Key Set - [Tata Motors, Reliance, Mahindra, Larsen, Birla, Adani]
Navigable Key Set - [Adani, Birla, Larsen, Mahindra, Reliance, Tata Motors]
Descending Map - {Tata Motors=10, Reliance=10, Mahindra=10, Larsen=2, Birla=10, Adani=10}
Head Map - {Adani=10, Birla=10, Larsen=2, Mahindra=10}
Head Map Including Key Passed - {Adani=10, Birla=10, Larsen=2, Mahindra=10, Reliance=10}
Tail Map Including Key Passed - {Reliance=10, Tata Motors=10}
Tail Map Without Key Passed - {Tata Motors=10}
Sub Map Including toKey - {Larsen=2, Mahindra=10, Reliance=10}
Sub Map without toKey and fromKey - {Mahindra=10, Reliance=10}
Ceiling Key - Reliance
Floor Key - Reliance
Higher Key - Tata Motors
Lower Key - Mahindra
Ceiling Entry - Reliance=10
Floor Entry - Reliance=10
Higher Entry - Tata Motors=10
Lower Entry - Mahindra=10
Poll First Entry - Adani=10
Poll Last Entry - Tata Motors=10







No comments:

Post a Comment