The “Collections” framework in Java came into action with the release of JDK 1.2 and was expanded quite a few times in Java 1.4 and Java 5 and then again in Java 6.
It provides you (the programmer) lists, sets, maps and queues to satisfy most of your coding needs. Moreover whichever one you pick is bound to give you at least a reasonable performance. Before we get into the depth of things let’s get a few things sorted out.
- collection (Lowercase c): This represents the different kind of data structures in which the objects are stored and iterated.
- Collection (Uppercase c): This is the interface from which Set, List and Queue Extend. “Syntax help:
- Collections (Uppercase c and ends with the character s): This is the class which holds all of the “static” utility methods for use with collections.
Now comes the question… What on earth do I do with a Collection?
- For starters, you can add objects to the collection.
- Remove objects from the collection
- You can check if you have a given object in a particular collection
- Retrieve any object from the collection without removing it. etc
Now, without further ado let’s get started with Collections. They usually come in four variants namely:
- Lists: List of things
- Sets: Unique things
- Maps: Things with a unique ID
- Queues: The First in First out thing.
The List Interface
The prime factor of importance in a List interface is INDEX. Having an index provides the programmer an entire arsenal of index methods. Secondly, all the three different list implementations are ordered by index positions and the name of these implementations are:
(I didn’t forget to press space in the above mentioned names…)
ArrayList: It can be thought of as a dynamic array and by dynamic array I mean an array which can grow and shrink according to requirements without having to explicitly specify it’s size.
The area in which ArrayLists help is in the field of FAST ITERATION and FAST RANDOM ACCESS. But the important thing to note here is that ArrayLists aren’t sorted by default.
- Vector: Vector is kind of like a slower ArrayList with the major difference that the Vector methods are all Synchronized for thread safety. (Now thread safety is a big topic in itself and we’ll not get into it right now.)
LinkedList: Once again we have something which resembles an ArrayList. The main difference being here the fact that the elements in a LinkedList are DOUBLY LINKED to each other.
The LinkedList iterate slower than the ArrayList, but it is the better choice when fast insertion or deletion is a requirement. We explain this in greater details in our tutorial Difference Between ArrayList and LinkedList in Java
The Set Interface
The word set here resembles the set which you might have studied in mathematics in your school, don’t start dreading over it if you didn’t like that chapter. The only thing of importance here is that in a Set we DO NOT allow duplicates. Just like the list interface, the set interface has three different implementations as well namely:
- HashSet: This is just an unsorted, unordered Set. The important thing here is that the LinkedHashSet uses the HASH CODE of the incoming object.
- LinkedHashSet: You must have guessed it by now that this is somewhat similar to HashSet and that’s absolutely correct so, I’ll just let you know the difference between these two which is (enter drum roll sound here) it is an ORDERED version of HashSet which maintains a DOUBLY-LINKED list across all elements.
- TreeSet: This is a SORTED collection and uses a red-black tree structure which guarantees that the elements will certainly be in ASCENDING ORDER according to the natural order.
The Map Interface
The map interface just cares about the fact that all the identifiers for all the objects which are either already present in the Collection or which are being added to the Collection are completely UNIQUE in nature. Here both the KEY and the VALUE are objects.
Like the above given two interfaces the Map interface has three different implementations as well namely:
HashMap: This gives you an UNSORTED, UNORDERD Map, where the keys land in the map is based on the key’s
HashTable: It is the Synchronized counterpart of the HashMap. It doesn’t allow anything that is
- LinkedHashMap: This implementation maintains insertion order which is a really important feature when we talk about iteration and it’s other uses, but has the much suspected disadvantage of being quite slow.
You may be also interested in this tutorial which explains the difference between HashMap and TreeMap in more details
The Queue Interface
Yes, we are talking about a normal queue here. The one’s which you encounter almost everywhere including the queue of results which were shown to you before you came to this article.
It basically follows the FIFO (First In First Out) principle
- PriorityQueue: The reason for the existence of a PriorityQueue is to create a “priority in, priority out” queue as opposed to the typical FIFO.
Secondly, in this implementation the elements are ordered by the NATURAL ORDERING or according to a Comparator.
These were the basic types of Collection in Java, this topic still has a lot more to cover and a lot more explanation has to happen, so don’t go pulling your hair off just yet if you don’t get all the differences between the different Collection implementations in your first go.
References: official Oracle Collections Tutorial