You are here
Home > Collections >

Improvements In Java Collection API

Improvements In Java Collection APIIn this article “Improvements in Java Collection API” we will learn about the origin of classes in different versions of jdk. Also, If any new class or interface introduced, what was the purpose of introducing that? What was the weakness of previous version of it? Why the new class/interface introduced? What all new features introduced in Collection classes/interfaces? and so on. We are going to discuss about these kinds of questions in this article accordingly. In fact, we will learn about Enhancement of java collection classes step by step & also version by version simultaneously. Primarily, we will focus on Java collection features in JDK 1.0, 1.2, 1.4, 1.5, 1.6 and 1.8, 9 10, 11, 21 respectively.

Improvements in Java Collection API

The Java Collection API has evolved significant improvements over the years, introducing new features, enhancements, and optimizations to enhance developer’s productivity, improve performance, and adapt to revised programming pattern and requirements. It will help developers to build more robust, efficient, and maintainable applications using the power of collections in Java.

Collection classes in JDK 1.0

In JDK 1.0 there were four classes Vector, Stack, Hashtable & Properties. In addition, there was one interface ‘Enumeration’ to iterate values in easy way. Further classification, Stack is a subclass of Vector & Properties is a subclass of Hashtable.

Problems with Vector class :

=>Vector is a thread-safe ie. All methods in the Vector are synchronized. So it is not good for single-threaded environment.
=>Since it works internally on arrays concept, insertion & deletion operations are very slow.
=>Even it allows adding duplicate elements in it.
=>Additionally, Vector can’t store elements in sorted order.

Problems with Hashtable class :

=>Hashtable is a thread-safe ie. all methods in Hashtable are synchronized. So it is not good for single-threaded environment.
=>Also, Hashtable can’t store entries in sorted order.

Problems with Enumeration:

=> It can’t delete elements & method names are large.

Collection classes in JDK 1.2

In JDK 1.2 Sun Micro-system had introduced ArrayList, LinkedList, HashSet, TreeSet, HashMap,TreeMap, Iterator & ListIterator.

ArrayList: Introduced to provide solution of single threaded environment as methods in ArrayList are not synchronized.

LinkedList: Introduced to provide faster insertion & deletion of elements.

HashSet: Introduced to provide solution of duplicate elements as there are no duplicates are allowed in HashSet.

TreeSet: Introduced to store elements in sorted order.

HashMap: Introduced to provide solution of single threaded environment as methods in HashMap are not synchronized.

TreeMap: Introduced to store elements in sorted order.

Iterator: Introduced to solve problems of Enumeration. Also has one class ListIterator to deal with List specially.

Problems with HashSet: It can’t maintain the insertion order ie. It doesn’t store the elements in the order they have added to the collection.

Problems with HashMap: Like HashSet, It can’t maintain insertion order.

Collection classes in JDK 1.4

In JDK 1.2 Sun Micro-system had introduced LinkedHashSet & LinkedHashMap.

LinkedHashSet: Introduced to solve the problem of insertion order in HashSet. It stores the elements in the order they have added to the collection.

LinkedHashMap: Introduced to solve the problem of insertion order in HashMap. It also stores the elements in the order they have added to the collection.

Collection classes in JDK 1.5

Introduced Queue & its subclasses.
for-Each loop: as an alternative approach to iterate in place of Iterator

CopyOnWriteArrayList: Introduced to allow for the safe iteration over the elements, even if we modify the underlying list.

CopyOnWriteArraySet: It uses an internal CopyOnWriteArrayList for all of its operations. So it shares the same basic properties of that.

Collection classes in JDK 1.6

NavigableSet: As a SortedSet extended with navigation methods reporting closest matches for given search targets.

NavigableMap: As a SortedMap extended with navigation methods returning the closest matches for given search targets.

Enhancement of Java Collection Classes

Collection classes in JDK 1.8

There are also new updates in the Java Collections Framework to support lambda expressions, streams, and aggregate operations.

♦ stream() as a default method in parent Interface Collection<E> : Returns a sequential Stream with this collection as its source.

♦ parallelStream() as a default method in parent Interface Collection<E> : Returns a possibly parallel Stream with this collection as its source.

♣ spliterator() as a default method in parent Interface Collection<E> : Creates a Spliterator over the elements in this collection.

♣ removeIf(Predicate<? super E> filter) as a default method in parent Interface Collection<E> : Removes all of the elements of this collection that satisfy the given predicate.

Equally important, one noticeable point here is that all newly added methods are default methods inside interface Collection<E>. This is the best example of default methods usage.

Collection Enhancements in Java 9

1) Introduction of new of() static factory methods for creating immutable lists, sets, and maps. These methods are:

 List.of(), Set.of(), Map.of(), Map.ofEntries() 

2) Arrays.mismatch(): Added new method to find the index of first mismatch between two arrays.

3) Arrays.compare(): Added new method to compare the elements of the two provided arrays.

4) Added more overloaded methods to the Arrays.equals().

5) Enumeration.asIterator(): Added new method that returns an instance of java.util.Iterator

Additionally, some methods like dropWhile, takeWhile, and ofNullable added under Enhanced Stream API .

Collection Enhancements in Java 10

Introduced List.copyOf(), Set.copyOf(), and Map.copyOf() for creating immutable copies of existing collections.

Collection Enhancements in Java 11

Collection.toArray(IntFunction): Added new default method that allows the collection’s elements to be transferred to a newly created array of a desired runtime type. The new method is an overloaded variant of the existing toArray(….) method.

Collection Enhancements in Java 21

Java 21 has introduced three new interfaces in Collection Framework: SequencedCollectionSequencedSet and SequencedMap. These new Collection interfaces offer us to access the first and the last element of it using default methods provided by new library. The feature also allows us to get a reversed view of the collection with a simple method call.

SequencedCollection 

default void addFirst(E e)
default void addLast(E e)

default E getFirst()
default E getLast()

default E removeFirst()
default E removeLast()

SequencedCollection<E> reversed()

SequencedSet

SequencedSet<E> reversed()

SequencedMap

default Map.Entry<K,V> firstEntry()
default Map.Entry<K,V> lastEntry()

default Map.Entry<K,V> pollFirstEntry()
default Map.Entry<K,V> pollLastEntry()

default V putFirst(K k, V v)
default V putLast(K k, V v)

SequencedMap<K,V> reversed()

default SequencedSet<Map.Entry<K,V>> sequencedEntrySet()
default SequencedSet<K> sequencedKeySet()
default SequencedCollection<V> sequencedValues()

For the detailed description, kindly visit the official documentation on SequencedCollection.

In order to learn Java Collection in detail, you may visit a separate article on Collection In Java.

Leave a Reply


Top