You don’t know Collections in JAVA !

Prem Parmar
4 min readSep 5, 2020

As a human being require water to survive, Java Developers must require to understand collection for surviving in a cut-throat IT industry.

Photo by Simon Daoudi on Unsplash

The only thing that absolutely have to know is, the location of the Library.

- Albert Einstein

Collection is the combination of interfaces and classes, which is use to store and process data efficiently. We can say that if you know collection, how to use collection, and when & where, which collection is used then you are “Programmer”.

Set, List, Queue, Map are the interfaces in collection.

List

  • List is ordered collection which means it is in sequence, you can access/insert any element in list by its position (aka index).
  • List interface is implemented by Classes : Array-List, Vector & Linked-List.
  • Array is group of same type element, which is static in size because of that Array-List is introduced which is combination of Array + Linked-List.
  • Array is fixed size and you never know how much data you need to store because of that waste of memory occurs at that time we use Linked-List.
  • There are some benefits of using Array as well like inserting element at the end or accessing data from Array has lower complexity than Linked-List. (Why at the end because when you insert element in the middle you have to switch other elements & same case with delete as well.)
  • In short, If you have accessing more element than the deleting-inserting at middle use Array vice versa.
  • Vector is one of the class which is synchronize which means you can access data of vector in single thread & Array-List is non-synchronize means it can be used by multi-thread. we will see this topic briefly in other story later.

Set

  • Set is unordered collection which can not contain duplicate values where list interface implemented classes contains duplicate values as well.
  • Set interfaces is implemented by Classes : Hash-Set, Tree-Set, Linked-Hash-Set.
  • First of all whenever any class implements collection interface (SLQM)and its prefix is “Tree” then it stores data in Red-Black tree in sorted manner.
  • Linked-Hash-Set stores data in insertion order means its in order of how your data is inserted and don’t store duplicate values.
  • Hash-Set stores data in Hash-Table. Obviously Tree-Set is slower than Hash-Set because its in sorted manner.

Map

  • Map stores data in key-value manner. Like Set, It is also unordered by index(position) but ordered by key. If you know Python then its Dictionary & If you know JSON then it is same.
  • Map interface is implemented by Classes : Hash-Map, Tree-Map, Linked-Hash-Map.
  • Map requires unique key and if you insert same key again then its value is replaced by older value means overwrites its value.
  • As discussed above, Tree-Map stores data in sorted manner in Red-Black Tree. Here as well it stores data in the sorted order of if its keys.
  • Linked-Hash-Map stores data in insertion order.

Queue

  • Queue stores data in FIFO (First-In, First-Out) manner. It means that the elements entered first are the deleted first.
Photo by Macau Photo Agency on Unsplash

Queue interface is implemented by Classes : Linked-List & Priority-Queue.

We have talked about linked list now its time to get some information about Priority-Queue.

Let us take real time example here :

Suppose you are going to railway station (GOA…🤔) and you are in queue of ticket booking counter. If you find old lady who is your behind then you tell her to please do move ahead first this is called “manner”. and you give priority to someone else in queue to do something.

  • Comparable Interface used to give priority to any element.

For accessing data we can use for Loop and Iterator as well. Let us take one example how to loop over through Array-List:

There is many ways to loop through list or any other objects but here are two ways :

  • Advance for Loop
  • Using Iterator

fail-fast

  • Fail-fast is type of iterator in which “ConcurrentModificationException” is thrown while iterating object and there is some modification like adding, deleting or updating.

This will throw error like this :

Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
at java.util.ArrayList$Itr.next(ArrayList.java:851)
at FailFastIterator.main(FailFastIterator.java:13)

fail-safe

  • Fail-safe is type of iterator in which “ConcurrentModificationException” is not thrown while iterating object and there is some modification like adding, deleting or updating. Its also known as “SAFE-Iterator”.

This is all about collection- interfaces in Java. We will see briefly about each and every objects in later stories. I also think to make Stories about some interview and tricky Programming question.

That’s it for to day. Beware, Be Safe and Wear a Mask😷.

--

--

Prem Parmar

Software Engineer, having 3 years of experience in Ecommerce / HCM domain Product based company.