How to choose Collection in Java? List, Set, Map, and Queue Example

The Java collection framework offers implementation of different data structure like an array, list, set, map, queue, tree, etc and the choice really depends upon the situation and properties of the different data structure. For example, if your requirement is fast search with index then you can use ArrayList and if you want to store key-value pairs then you would consider using hash table data structure and there are a couple of implementation of hash table data structure in Java, like HashMap, Hashtable, LinkedHashMap, TreeMap, and ConcurrentHashMap. Now, which one will you choose? If you don't know or confused don't worry, I will give you a set of rules and use cases which will help you choose the right Collection type in Java depending upon scenario. 


Again if you need simple key-value pair storage then you using HashMap is fine but if you are concerned about thread-safety then you have three more options, synchronized HashMap,  Hashtable and ConcurrentHashMap, the latter being better because of more Scalability.

Same if you are concern about the order because the map doesn't guarantee any order you can either use LinkedHashMap or TreeMap, the LinkedHashMap provides insertion order guarantee like it keeps mappings in the order they are inserted while TreeMap is a sorted map which keeps mapping in the sorted order defined by their Comparator or Comparable implementation.

So, it all boils down to your requirement and your understanding of different Collection classes in Java. In this article, I'll give you a brief and comparative overview of essential interfaces from the Java collection framework and highlight their unique properties which will help you to choose the right Collection class for the job.




What is the Java Collection Framework? Why you should Learn it?

The Java Collection framework is one of the most important API in JDK. It defines the implementation of the various data structure in Java language which makes it really important for Java developers to learn and understand.

You can think of its popularity in the terms that there is hardly any Java application written which has not used any classes from the Java collection framework.

All important classes which you have used or heard like ArrayList, HashMap, Vector, LinkedList, Deque,BlockingQueueall come from the Java collection framework.

In order to effectively use the Java Collection framework and choose the right type of Collection for the job, you need to know 4 basic interfaces like list, set, map, and queue.
  • A List is an ordered collection that allows duplicates.
  • A Set is an unordered collection that doesn't allow duplicate.
  • A Map associates one object to another and provides an implementation of hash table data structure. It doesn't guarantee any order.
  • A Queue is an interface which provides FIFO ordering of elements.

And, here is a quick guide of how to choose the right Collection for the job in Java:

How to Choose the Right Collection in Java? List, Set, Map, and Queue Example


1. List 

The List interface then has several implementations but two of the most important ones are ArrayList and LinkedList. Former represent an array data structure while later represents a linked list data structure. 

Just like the array is suitable for fast search, an ArrayList should be used if you need fast search and your data may or may not contain duplicates.

A LinkedList is better for frequent addition and deletion but provides slow access, hence if you are processing data and frequently adding and removing from a list then the linked list can be a good choice. If you want to know more about them, I suggest you read when to ArrayList and LinkedList in Java.

Difference between List, ArrayList, and LinkedList in Java




2. Set

The Set interface has three main implementations HashSet, TreeSet, and LinkedHashSet. The HashSet is based upon Hashtable but it doesn't associate one object to other, it just stores them with default values. If you need a general-purpose set to store unique values, you can use HashSet.

LinkedHashSet provides ordering and uniqueness and should be used to maintain unique elements in the order they are inserted into the set.

The TreeSet arrange elements in the order defined by their Comparator or Comparable and if you need to keep elements sorted, this is the set you can use. Here is a nice tabular summary of difference between HashSet, TreeSet, and LinkedHashSet as well other different types of Set classes in Java on different parameters e.g. data structure, sorting, iterator and how they handle null input.

Difference between LinkedHashSet, TreeSet and HashSet in Java




3. Map

The Map interface also comes with a couple of implementations e.g. HashMap, Hashtable, and LinkedHashMap.

The HashMap is your general-purpose if you need to associate one object to other e.g. id to name then you can use this. It provides a fast lookup if you know the key but it doesn't guarantee any order.

The LinkedHashMap solves that problem by providing an ordering guarantee because it keeps all mappings in the order they are inserted into the map.

The Hashtable is a synchronized version of HashMap, it's a legacy class and best to avoid in favor of more scalable alternatives like ConcurrentHashMap.

There is also a TreeMap which like TreeSet keeps mappings in the sorted order defined by their Comparator and Comparable implementations.  Here is a nice table of differences between HashMap, LinkedHashMap, and TreeMap in Java:

Difference between TreeMap, LinkedHashMap and HashMap in Java



4. Queue

The Queue interface provides a FIFO structure like First In First Out. There are a couple fo queue implementations available in JDK e.g. PriorityQueue, BlockingQueue, and LinkedList.

You can use PriorityQueue to process elements in priority e.g. always processing a high priority item first. It's good for scheduling jobs and other things that are processed on priority.

A BlockingQueue is used for thread-safety and workflow. You can use it to implement a Producer-consumer pattern like where one thread can add objects into the queue and others can consume it.

How to use BlockingQueue in Java




That's all about how to choose the right type of Collection in Java. As I told you in the first paragraph the Java Collection framework provides implementations of several data structures like hash tables, list, set, priority queue, stack, queue, deque, etc. Now, the most important question is how do you choose a collection? Well, it entirely depends upon your need.

This is just an example, in this article, we are going to explore other important Collection classes to learn how to choose the right type of Collection for the job.

Other Java Collections Tutorial and Interview Questions
  • Difference between HashSet, TreeSet, and LinkedHashSet in Java? (answer)
  • 50 Java Programs from Coding Interviews (list)
  • Difference between HashMap and ArrayList in Java? (answer)
  • 10 Java Garbage Collection Interview Questions (list)
  • What is the difference between HashSet and ArrayList in Java? (answer)
  • 21 Java Final modifier Interview Questions (list)
  • What is the difference between ArrayList and LinkedList in Java? (answer)
  • 21 Java Inheritance Interview Questions with Answers (list)
  • 5 differences between HashMap and Hashtable in Java? (answer)
  • 10 Date, Time, and Calendar based Interview Questions with Answers (list)
  • How to use NavigableMap in Java 6? [example]
  • 10 Example of ConcurrentHashMap in Java? (concurrenthashmap)
  • How to use BlockingQueue in Java Program? [example]
  • 25 Java Collection Interview Questions with Answers (collections)
  • Top 5 Courses to learn Java Collections Framework in-depth (courses)

Thanks for reading this article so far. If you like this tutorial then please share with your friends and colleagues. If you have any questions or feedback then please drop a note.


No comments:

Post a Comment