Thursday, September 6, 2007

Java 6.0 New Collection APIs

Java 6.0 New Collection APIs

The following are the new collection APIs introduced in Java 6.0. I listes them as Interfaces and classes.

New Interfaces
  • Deque
  • BlockingDeque
  • NavigableSet
  • NavigableMap
New Classes
  • ArrayDeque
  • LinkedBlockingDeque
  • ConcurrentSkipListSet
  • ConcurrentSkipListMap
  • AbstractMap.SimpleEntry
  • AbstractMap.SimpleImmutableEntry
Updated Classes in Java 6.0
  • LinkedList
  • TreeSet
  • TreeMap
  • Collections
Deque and ArrayDeque
Deque is the abbreviation of “Double Ended Queue”. A Collection that allows us to add (or) remove elements at both ends. Deque supports total size of collection for both fixed and unspecified size limits.
Deque implementation can be used as Stack(Last in first out ) or Queue(First in First Out). For each insertion, retrieval and removal of elements from deque there exists methods in 2 flavours. One will throw exception if it fails in an operation and another one returns status or special value for each operation.
Operation Special value method Exception throwing method

Operation Special value method Exception throwing method

Insertion at head offerFirst(e) addFirst(e)
Removal at head pollFirst() removeFirst()
Retrieval at Head peekFirst() getFirst()
Insertion at Tail offerLast(e) addLast(e)
Removal at Tail pollLast() removeLast()
Retrieval at Tail peekLast() getLast()
Implementation of Deque doesn’t require preventing the insertion of null, but when we are using special value method null is return to indicate that collection is empty. So it is recommendable not to allow insertion of null.

ArrayDeque is a class that implements Deque. It has no capacity restrictions. It will perform faster than stack when used as stack and faster than linked list when used as queue. ArrayDeque is not thread Safe. The following example explains how to write program using ArrayDeque.

Example

import java.util.ArrayDeque;
import java.util.Iterator;

public class DequeExample
{
public static void main(String as[])
{
ArrayDeque adObj = new ArrayDeque();
//Insertion by using various methods

adObj.add("Oracle");
adObj.addFirst("DB2");
adObj.offerFirst("MySQL"); //returns boolean - true R false
adObj.offerLast("Postgres"); //returns boolean - true R false

//Retrievals

System.out.println("Retrieving First Element :" + adObj.peekFirst());
System.out.println("Retrieving Last Element :"+ adObj.peekLast());

//Removals

System.out.println("Removing First Element :"+ adObj.pollFirst());
System.out.println("Removing Last Element :"+ adObj.pollLast());

//Reverse traversal

System.out.println("Remaining Elements :");
Iterator it = adObj.descendingIterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}

Output:

Retrieving First Element :MySQL
Retrieving Last Element :Postgres
Removing First Element :MySQL
Removing Last Element :Postgres
Remaining Elements :
Oracle
DB2

BlockingDeque and LinkedBlockingDeque

A BlockingDeque is similar to Deque and provides additionally functionality. When we tries to insert an element in a BlockingDeque, which is already full, it can wait till the space becomes available to insert an element. We can also specify the time limit for waiting.

0 comments:

BidVertiser