You are here
Home > Collections >

Enhancement of Java Collection Classes


Enhancement of Java Collection Classes
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 introduced, what was the purpose of introducing that? What was the weakness of previous version of the class? Why the new class introduced? What all new features introduced in Collection classes? 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. Particularly, we will focus on Java collection classes features in JDK 1.0, 1.2, 1.4, 1.5, 1.6 and 1.8 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.

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

Leave a Reply

Top