Enhancement 1.6 version

Enhancement

Sun people introduces, the following 2 interface in 1.6 version as the part of collection Framework.

1) NavigableSet

2) NavigableMap.

NavigableSet

It is the child interface of SortedSet, which defines several methods to support navigation.

  1. Ceiling(e): It returns the lowest element, which is greater than equal to e.
  2. Higher(e): Returns the lowest element which is greater than ‘e’.
  3. Floor(e): Returns highest element which is less than or equal to e.
  4. Lower(e): Returns highest element which is less than.
  5. PollFirst(): Returns & removes the First element.
  6. PollLast(): Returns & removes the Last element.
  7. DescendingSet(): Returns the NavigableSet, in reverse order.

NavigableSetDemo.java

package com.narayanatutorial.collections.set;

import java.util.TreeSet;

public class NavigableSetDemo {

 public static void main (String[] args)             
 {           
	 TreeSet<Integer> t = new TreeSet<Integer> ();               
	 t.add(1000);                       
	 t.add(2000);                
	 t.add(3000);            
	 t.add(4000);                
	 t.add(5000);           
	 System.out.println("treeset:"+t); // 1000,2000,3000,4000,5000          
	 System.out.println("ceiling:"+t.ceiling(2000)); //2000           
	 System.out.println("higher:"+t.higher(2000)); //3000           
	 System.out.println("floor:"+t.floor(3000)); //3000          
	 System.out.println("lower:"+t.lower(3000)); //2000          
	 System.out.println("pollFirst:"+t.pollFirst()); //1000          
	 System.out.println("pollLast:"+t.pollLast()); //5000           
	 System.out.println("Final treeset:"+t); //2000,3000,4000           
	 }
 }

Output

treeset:[1000, 2000, 3000, 4000, 5000]
ceiling:2000
higher:3000
floor:3000
lower:2000
pollFirst:1000
pollLast:5000
Final treeset:[2000, 3000, 4000]

NavigableMap

It is child interface of SortedMap. It defines several method to support Navigation.

Map⇒ SortedMap⇒ NavigableMap⇒ TreeMap.

  • CeilingKey (e)
  • HigherKey (e1)
  • FloorKey (e1)
  • LowerKey (e1)
  • PollFirstEntry ()
  • PollLastEntry()
  • DescendingMap()

NavigableMap.java

package com.narayanatutorial.collections.set;

import java.util.TreeMap;

public class NavigableMap {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		TreeMap<String, String> t = new TreeMap<String, String>();
		t.put("b", "banana");
		t.put("a", "apple");
		t.put("d", "dog");
		t.put("c", "cat");
		t.put("g", "gun");
		System.out.println("TreeMap:"+t);
		System.out.println("ceilingKey:"+t.ceilingKey("c"));
		System.out.println("higherKey:"+t.higherKey("e"));
		System.out.println("floorKey:"+t.floorKey("e"));
		System.out.println("lowerKey:"+t.lowerKey("e"));
		System.out.println("pollFirstEntry"+t.pollFirstEntry());
		System.out.println("pollFirstEntry:"+t.pollLastEntry());
		System.out.println("pollFirstEntry:"+t.descendingMap());
		System.out.println("final TreeMap"+t);

	}

}

Output

TreeMap:{a=apple, b=banana, c=cat, d=dog, g=gun}
ceilingKey:c
higherKey:g
floorKey:d
lowerKey:d
pollFirstEntrya=apple
pollFirstEntry:g=gun
pollFirstEntry:{d=dog, c=cat, b=banana}
final TreeMap{b=banana, c=cat, d=dog}

Leave a Reply