Features and Enhancement in jdk1.6

Here are some of the features and enhancements in jdk1.6. And we will explore them further in this chapter:

Only important features we need for interview all other features you will understand as you get experience in industry.

  • Collections Framework
  • Deployment (Java Web Start and Java Plug-in)
  • Drag and Drop
  • Instrumentation
  • Internationalization Support
  • I/O Support
  • JAR (Java Archive Files) - An annotated list of changes between the 5.0 and 6.0 releases to APIs, the jar command, and the jar/zip implementation.
  • Java Web Start
  • Java DB 10.2 JDBC4 Early Access
  • JMX (Java Management Extensions) - A list of JMX API changes between the J2SE 5.0 and Java SE 6 releases
  • JPDA (Java Platform Debugger Architecture)
  • JVM TI (Java Virtual Machine Tool Interface)
  • lang and util Packages
  • Monitoring and Management for the Java Platform
  • JConsole is Officially Supported in Java SE 6
  • Networking Features
  • Performance
  • Reflection
  • RMI (Remote Method Invocation)
  • Scripting
  • Security
  • Serialisation of Objects
  • Swing
  • VM (Java Virtual Machine)

“The collections framework is a unified architecture for representing and manipulating collections, allowing them to be manipulated independently of the details of their representation. It reduces programming effort while increasing performance.

It allows for interoperability among unrelated APIs, reduces effort in designing and learning new APIs, and fosters software reuse.”

These new collection interfaces provided:

  • Deque - a double ended queue, supporting element insertion and removal at both ends. It extends the Queue interface.
  • java.util.Deque interfaces is a subtype of the java.util.Queue interface.
  • It is also called as deque.

Here is a list of topics that will be covered in this chapter:

  • Deque Implementations.
  • Adding and accessing elements.
  • Removing elements.
  • Generic Deques.

Deque Implementations can be implemented by a doubly linked list or circular array

  • Being a Queue subtype, all methods of Queue and Collection interfaces are also available in the Deque interface.
  • There are two ways to use it:
    • java.util.ArrayDeque
    • java.util.LinkedList
  • Array Deque stores its elements in an array, and as soon as it reaches its length it assigns it to a new array.

To create a Deque instance, following the steps given below:


Adding and Accessing Elements


Removing Elements

The generic deque is used to make sure which type of objects get inserted into deque.

This Deque can now only have MyObject instances inserted into it. You can then access and iterate its elements without casting them.

Here is how it looks:

A blocking deque supports blocking operations.

  • It is a Deque with operations that wait for the deque to become non- empty when retrieving an element.

  • Additionally, it waits for space to become available in the deque when storing an element.

  • It extends both the Deque and BlockingQueue interfaces (This interface is part of java.util.concurrent).

  • It is also thread Safe.

  • It does not allow null elements in.

  • It may or may not be capacity-constrained.

  • A BlockingDeque implementation may be used directly as a FIFO BlockingQueue.

It refers to a navigable set within the collections framework.

  • It is a SortedSet extended with navigation methods reporting closest matches for given search targets.

  • A NavigableSet may be accessed and traversed in either ascending or descending order depending on the requirement.

  • This interface is intended to supersede the SortedSet interface.

  • It knows all Superinterfaces :
    Collection<E>, Iterable <E>, Set<E>, Sorted Set<E>
  • It knows all Implementing Classes : ConcurrentSkipListSet, TreeSet.

It is a subtype of the SortedMap interface and makes navigation methods more convenient. It also has an added feature to make a submap from an existing map.

  • A Navigable map is a SortedMap extended with navigation methods which returns the closest matches for given search targets.

  • A NavigableMap may be accessed and traversed in either ascending or descending key order.

  • This interface is intended to supersede the SortedMap interface.

  • Interface NavigableMap< K,V >: K is the type of key maintained by this map and V is the type of mapped values

  • It knows all Superinterfaces : Map< K,V > , SortedMap< K,V >.

  • It knows all SubInterfaces : ConcurrentNavigableMap< K,V >

  • It knows all known Implementing Classes: ConcurrentSkipListMap, TreeMap

The ConcurrentNavigableMap provides support for concurrent access for the submaps. (This interface is a part of java.util.concurrent).

  • The ConcurrentMap is also a NavigableMap.

  • It knows all Superinterfaces:
    ConcurrentMap< K,V >,Map< K,V >, NavigableMap< K,V >, SortedMap< K,V >

  • The following concrete implementation classes have been added:
    ArrayDeque, also known as Array Double Ended Queue or Array Deck, is an efficient resizable-array implementation of the Deque interface.

  • Concurrent Skip List Set - concurrent scalable skip list implementation of the Navigable Set interface.

  • Concurrent Skip List Map - concurrent scalable skip list implementation of the ConcurrentNavigableMap interface.

  • Linked Blocking Deque - concurrent scalable optionally bounded FIFO blocking deque backed by linked nodes.

  • Abstract Map. Simple Entry - simple mutable implementation of Map.Entry.

  • Abstract Map.SimpleImmutable Entry - simple immutable implementation of Map.Entry.

  • These existing classes have been updated to implement new interfaces:

    • LinkedList
    • TreeSet
    • TreeMap
    • Collections

Two new methods were added to the Collections utility class:

  • new Set From Map (Map) – This creates a general purpose Set implementation from a general purpose Map implementation

There is no IdentityHashSet class, instead, you may use
Set< Object > identityHashSet= Collections.newSetFromMap(new IdentityHashMap< Object, Boolean>());

  • asLifoQueue(Deque) - returns a view of a Deque as a Last-in- first-out (LIFO) Queue
  • The Arrays utility class now has methods copyOf and copyOfRange that can efficiently resize, truncate, or copy subarrays for arrays of all types.
  • Deque is the abbreviation of "Double Ended Queue". It is a collection that allows us to add (or) remove elements at both ends. Deque supports the 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 are two different methods. One will throw exception if it fails in an operation and the other returns the status or special value for each operation.

  • Operation Special value method Exception throwing method
      Insertion at head
      Removal at head
      Retrieval at Head
      Insertion at Tail
      Removal at Tail
      Retrieval at Tail
      offerFirst(e)
      pollFirst()
      peekFirst()
      offerLast(e)
      pollLast()
      peekLast()
      addFirst(e)
      removeFirst()
      getFirst()
      addLast(e)
      removeLast()
      getLast()

  • Implementation of Deque doesn't require preventing the insertion of null, but when we are using special value method, null is returned to indicate that the collection is empty. So, it is recommendable to not 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 stackand faster than linked list when used as queue. ArrayDeque is not thread safe.

The following example explains how to write program using ArrayDeque:

  • A blocking deque is basically a deque that waits for a deque to be non-empty while it retrieves an element. It will also wait for an empty space in the deque before storing an element.

  • A BlockingDeque is similar to Deque and provides additional functionality. When we try 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.

  • There are four BlockingDeque methods:
    • Methods throws exception
    • Methods returns special value
    • Methods that blocks (Waits indefinitely for space to available)
    • Methods that times out (Waits for a given time for space to available)

Operation Special Value Throws Exception Block Times Out
Insertion Head Add First(e) Offer First(e) Put First(e) Offer First(e, time, unit)
Removal from head Remove first() Poll First() Take First() Take First(Time, Unit)
Retrieval from head Get First() Peak First() NA NA
Insertion at tail Add Last(e) Offer Last(e) Put Last(e) Offer Last(e, Time, Unit)
Removal from tail RemoveLast() Poll Last() Take Last() Take First (Time, Unit)
Retrieval from tail Get Last() Peak Last() NA NA

A LinkedBlockingDeque is a Collection class, which implements BlockingDeque interface in which we can specify maximum capacity if we want. If we did not specify the capacitythen the maximum capacity will be Integer.MAX_VALUE by default.

Example:

“A navigable set is a sorted set that lets you work with its subsets in a variety of ways.” Suppose we have a requirement to:

  • Retrieve the element which is immediately greater than or lower than 15 from the given set: [5,10,15,20]

  • Retrieve all elements greater than or lower than 10.
    With the help of existing methods we need to take few risks to achieve them. But with NavigableSet methods it becomes just a method call.
    The NavigableSet method is used to return the closest matches of elements for the given elements in the collection.
    The ConcurrentSkipListSet is one of the classes that implements NavigableSet.

  • Navigable set has the following interfaces: Collection< E >, Iterable< E >, Set< E >, SortedSet< E >, All Known implementing classes ConcurrentSkipListSet, TreeSet


What are the salient features of NavigableMap and ConcurrentSkipListMap and how do they differ?

  • NaviagableMap is similar to NaviagableSet

  • ConcurrentNavigableMap< K,V >

  • In NavigableSet, methods are used to return values, but in NaviagableMap, methods used to return the key, value pair.

  • ConcurrentSkipListMap is the one of the classes which implements NaviagableMap.

  • It knows all Superinterfaces Map< K,V >, SortedMap< K,V >

  • It knows all known implementing Classes ConcurrentSkipListMap, TreeMap.