In this article “Enhancement of Java Collection classes” 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, 14, 16, 17, 21 respectively.
What is Collection in Java?
As we might already be knowing, there are multiple ways to store values in java ie. Variables, arrays, classes. On the other hand, we also have collections to store values in very flexible & faster way to retrieve them. We can store only a single value in variable. Further, we have Arrays to store multiple values of the same type but fixed in numbers. In a class, we can store multiple values of different types, but again fixed in numbers. But in Collections we can store any number of values and also of different types.
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.
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
- Introduction of new of() static factory methods for creating immutable lists, sets, and maps.
- Enhanced Stream API with methods like dropWhile, takeWhile, and ofNullable.
- New methods added to the Collection interface, such as forEach(Consumer).
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
Added new default Method: Collection.toArray(IntFunction) 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 16
Enhanced the List interface with replaceAll(UnaryOperator) and sort(Comparator) methods.
Collection Enhancements in Java 17
Introduced List.of() and Set.of() methods with copy semantics, making it easier to create defensive copies of collections.
Collection Enhancements in Java 21
Java 21 has introduced three new interfaces in Collection Framework: SequencedCollection, SequencedSet 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.
default void addFirst(E e)
default void addLast(E e)
default E getFirst()
default E getLast()
default E removeFirst()
default E removeLast()
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)
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.