NavigableMap


headMap(), tailMap() and subMap()

 

The first interesting navigation methods are the descendingKeySet() and descendingMap() methods.

The descendingKeySet() method returns a NavigableSet in which the order of the elements is reversed compared to the original key set. The returned "view" is backed by the original NavigableSet ket set, so changes to the descending set are also reflected in the original set. However, you should not remove elements directly from the key set. Use the Map.remove() method instead.

 

Here is a simple example:

 

NavigableSet reverse = map.descendingKeySet();

 

The descendingMap() method returns a NavigableMap which is a view of the original Map. The order of the elements in this view map is reverse of the order of the original map. Being a view of the original map, any changes to this view is also reflected in the original map.

 

Here is a simple example:

 

NavigableMap descending = map.descendingMap();

 

 

 

headMap(), tailMap() and subMap()

 

The headMap() method returns a view of the original NavigableMap which only contains elements that are "less than" the given element.

 

Here is an example:

NavigableMap original = new TreeMap();

original.put("1", "1");

original.put("2", "2");

original.put("3", "3");

 

//this headmap1 will contain "1" and "2"

SortedMap headmap1 = original.headMap("3");

 

//this headmap2 will contain "1", "2", and "3" because "inclusive"=true

NavigableMap headmap2 = original.headMap("3", true);

 

The tailMap() method works the same way, except it returns all elements that are higher than the given parameter element.

The subMap() allows you to pass two parameters demarcating the boundaries of the view map to return.

 

Here is an example:

NavigableMap original = new TreeMap();

original.put("1", "1");

original.put("2", "2");

original.put("3", "3");

original.put("4", "4");

original.put("5", "5");

 

// this submap1 will contain "3", "3"

SortedMap submap1 = original.subMap("2", "4");

 

// this submap2 will contain ("2", "2") ("3", "3") and ("4", "4") because

// fromInclusive=true, and toInclusive=true

NavigableMap submap2 = original.subMap("2", true, "4", true);

 

 

 

ceilingKey(), floorKey(), higherKey() and lowerKey()

 

The ceilingKey() method returns the least (smallest) key in this map that is greater than or equal to the element passed as parameter to the ceilingKey() method.


 Here is an example:

NavigableMap original = new TreeMap();

original.put("1", "1");

original.put("2", "2");

original.put("3", "3");

 

// ceilingKey will be "2".

Object ceilingKey = original.ceilingKey("2");

 

// floorKey will be "2".

Object floorKey = original.floorKey("2");

 

The floorKey() method does the opposite of ceilingKey()

The higherKey() method returns the least (smallest) element in this map that is greater than (not equal too) the element passed as parameter to the higherKey() method.


Here is an example:

 

NavigableMap original = new TreeMap();

original.put("1", "1");

original.put("2", "2");

original.put("3", "3");

 

// higherKey will be "3".

Object higherKey = original.higherKey("2");

 

// lowerKey will be "1"

Object lowerKey = original.lowerKey("2");

 

 The lowerKey() method does the opposite of the higherKey() method.



 

celingEntry(), floorEntry(), higherEntry(), lowerEntry()

 

The NavigableMap also has methods to get the entry for a given key, rather than the key itself. These methods behave like the ceilingKey() etc. methods, except they return an Map.Entry instead of the key object itself.

Map.Entry maps a single key to a single value.

Here is a simple example. For more details, check out the JavaDoc.

 

NavigableMap original = new TreeMap();

original.put("1", "1");

original.put("2", "2");

original.put("3", "3");

 

// higherEntry will be ("3", "3").

Map.Entry higherEntry = original.higherEntry("2");

 

// lowerEntry will be ("1", "1")

Map.Entry lowerEntry = original.lowerEntry("2");

 


pollFirstEntry() and pollLastEntry()

 

The pollFirstEntry() method returns and removes the "first" entry (key + value) in the NavigableMap or null if the map is empty. The pollLastEntry() returns and removes the "last" element in the map or null if the map is empty. "First" means smallest element according to the sort order of the keys. "Last" means largest key according to the element sorting order of the map.

Here are two examples:

 

NavigableMap original = new TreeMap();

original.put("1", "1");

original.put("2", "2");

original.put("3", "3");

 

// first is ("1", "1")

Map.Entry first = original.pollFirstEntry();

 

// last is ("3", "3")

Map.Entry last = original.pollLastEntry();

 

 

NavigableMap 예제

 

import java.util.NavigableMap;

import java.util.TreeMap;

import java.util.concurrent.ConcurrentSkipListMap;

 

/**

 * @author Steven J.S Min

 *

 */

public class NavigableMapExample {

 

       /**

        * @param args

        */

       public static void main(String[] args) {

 

              // NavigableMap<Integer, String> navMap = new ConcurrentSkipListMap<Integer, String>();

              NavigableMap<Integer, String> navMap = new TreeMap<Integer, String>();

 

              navMap.put(1, "January");

              navMap.put(2, "February");

              navMap.put(3, "March");

              // navMap.put(4, "April");

              // navMap.put(5, "May");

              navMap.put(7, "July");

              navMap.put(6, "June");

              navMap.put(8, "August");

              navMap.put(9, "September");

              navMap.put(10, "October");

              navMap.put(12, "December");

              navMap.put(11, "November");

 

              System.out.println("Data in the navigable map: " + navMap.descendingMap() + "\n");

 

              System.out.print("First data: " + navMap.firstEntry() + "\n");

              System.out.print("Last data: " + navMap.lastEntry() + "\n\n");

 

              System.out.print("Nearest less than or equal to the given key: " + navMap.floorEntry(5) + "\n");

              System.out.println("Retrieving the greatest key strictly less than the given key: " +

                      navMap.lowerEntry(3));

              System.out.println("Retriving data from navigable map greter than  the given key: " +

                      navMap.higherEntry(5) + "\n");

 

              System.out.println("Removing First: " + navMap.pollFirstEntry());

              System.out.println("Removing Last: " + navMap.pollLastEntry() + "\n");

 

 

              System.out.println("Now data: " + navMap.descendingMap());

 

       }

}

 

 

결 과

Data in the navigable map: {12=December, 11=November, 10=October, 9=September, 8=August, 7=July, 6=June, 3=March, 2=February, 1=January}

 

First data: 1=January

Last data: 12=December

 

Nearest less than or equal to the given key: 3=March

Retrieving the greatest key strictly less than the given key: 2=February

Retriving data from navigable map greter than  the given key: 6=June

 

Removing First: 1=January

Removing Last: 12=December

 

Now data: {11=November, 10=October, 9=September, 8=August, 7=July, 6=June, 3=March, 2=February}

 

 

'Java > Core Utilities' 카테고리의 다른 글

Synchronized and Unsynchronized Collections  (0) 2013.11.04
Observers and Observables  (0) 2013.02.09
Logger  (0) 2013.02.09
Preferences & PreferenceChangeListener  (0) 2013.02.09
Properties  (0) 2013.02.09
Posted by Steven J.S Min
,