Magazine
 
Java Collections API
 

insert an element. There are four operations defined for
each method specified in the BlockingDeque.


•Method may throw exception.
•Method may wait for a given time if times-out.
•Method that blocks may wait indefinitely for space to
be available.
•Method returns special value.
3. NavigableSet : It is used to return the closest
matches of elements. For instance if it is needed to
retrieve an element, which is immediately greater than,
or lower than element 20 or if we want to retrieve all
elements greater than or lower than 35 from sorted set
elements [10,20,35,5] we can use NavigableSet.
ConcurrentSkipListSet is one of the class that implements
NavigableSet.

4. NavigableMap: Unlike the NavigableSet (return
values), NaviagableMap methods is used to return the
key-value pair. ConcurrentSkipListMap is the one of
the class which implements NaviagableMap

New classes have been introduced in Java 6.0
collections APIs. These are:

1. ArrayDeque: ArrayDeque is a class that implements
Deque. If used as a stack or linked list, it performs
much faster. It is neither thread safe nor does it has the
capacity restrictions.

2. LinkedBlockingDeque: It implements
BlockingDeque. Maximum capacity can be specified by
using BlockingDeque interface. However the maximum
capacity will be Integer.MAX_VALUE if not specified.

3. ConcurrentSkipListSet: ConcurrentSkipListSet is
one of the class that implements NavigableSet. It is used
to return the closest matches of elements.


4. ConcurrentSkipListMap: ConcurrentSkipListMap
is one of the class which implements NaviagableMap. In
NavigableSet, methods are used to return values.

5. AbstractMap.SimpleEntry: The key-value pair of
a single entry in a Map is held by the instance of this
class. Moreover it is a static nested class which is nested
inside an abstractMap class.

 

6. AbstractMap.SimpleImmutableEntry: This class
is similar to AbstractMap.SimpleEntry class however it has
one difference that it throws the exception UnsupportedOperationException when we try to set an
improper value.

Modified Classes in Java 6.0 collections APIs
• LinkedList
• TreeSet
• TreeMap
• Collections

Some existing classes have been modified by implementing
the new interfaces. TreeSet class is have
implemented the NavigableSet, LinkedList class is
modified to implement Deque, similarly TreeMap is
modified to implement NavigableMap etc. Some of the
new methods like newSetFromMap and asLifoQueue
have been added to Collections 2.

Bi-directional traversal has become easier with java
6.0 collection APIs.

Let’s see the features of the NavigableMap
using an example.

In the example below the NavigableMap methods are
used to return the key-value pair element from the
specified collection object location like
retrieving first key-value pair, retrieving last key-value
pair, retrieving the greatest key strictly less than the
given key, retrieving a key-value associated with the least
key strictly greater than the given key, removing the first
key-value pair, removing the last key-value pair etc..

import java.util.*;
import java.util.concurrent.*;
public class NavigableMapExample
{
public static void main(String[] args)
{
System.out.println(“Navigable Map Example!\n”);
NavigableMap <Integer, String>navMap =
new ConcurrentSkipListMap<Integer, String>();
navMap.put(1, “January”);
navMap.put(2, “February”);
navMap.put(3, “March”);
navMap.put(4, “April”);
navMap.put(5, “May”);
navMap.put(6, “June”);
navMap.put(7, “July”);
navMap.put(8, “August”);
navMap.put(9, “September”);
navMap.put(10, “October”);
navMap.put(11, “November”);
July 2007 | Java Jazz Up |13
 
previous
index
next
 
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 ,            Download PDF