Java Collection Framework: Use Case and Types

Shubham Rawat
Coding Blocks
Published in
6 min readApr 29, 2020

--

Stuck ………….What a Java Collection framework is and why use them?

Don’t worry, I got your back. We will be going through a series of blogs that will take you through a wonderful journey of what Java Collection Framework is, their classifications, and when to use what.

INTRODUCTION

Java Collections Framework is a very important framework which many developers use nowadays. Being a good Java Developer it is suggested that you should have a pretty good knowledge of what the collections are and how they are used.

So, Before moving to Java collections lets first discuss what do you understand by the term collections. Take your time to think, don’t look down immediately. 🤔

So, If you are here then I probably guess that you may have thought what a collection means. Yes, great you are right. A collection means a bundle of objects. for eg: a collection of CDs, a collection of coins, or maybe a collection of your favorite TV series.

Being a programmer, what you might be thinking is that for making a collection of something we could have used an array. It is a collection of homogenous data types. Isn’t that the solution to my problem. So why should I use this big thing named Collection framework? How is it helping me out?

Ahh, this is a problem that arises in the mind of many programmers so let me present you some scenarios where using an array will not be as friendly as a Collection framework could be.

At the end of this article, I believe, I’ll be able to convince you that yes nothing is as good as Collection frameworks in Java.

WHY NOT ARRAY?

I hope you have an idea about what an array is. At least that much part you are aware with that can help us to proceed with our problem scenarios:-

  • So, here is our first problem that arises at the time of the initialization of an array. So the question that is arising in your mind is what is that problem? So the answer to this is simple, at the time of initialization of an array, you need to give a specific size which also somehow bounds your array to a fixed size, and once initialized you can’t change its size. Do you want that?
Fig 1. Array Size Required
  • Let’s talk about our second problem that comes into the scene when you want to add extra elements to the array. It is somehow related to the first problem. If the size of your array is ’n’, then you can’t put (n+1)th elements in this array as the array is not resizable.
    But, In this dynamically changing world, would you like to have such behavior with your collection of data. Obviously, with increasing market demands and need for storage, you will have to keep updating the data and add more and more to it.
Fig 2. Cannot add extra elements as arrays are static in size
  • Agree or not, but arrays are sometimes not very user friendly. Try to print your array directly and you will get to see some alien language. Jokes apart, it’s just printing a hashcode of that array. Those who are a newbie to programming they might have a hard time understanding what this hashcode is.
Fig 3. Not so user-friendly
  • Last but not least, there can be a problem with uniqueness. This problem arises at the times when you want your array to contain unique elements only so that you might need to write some good logic to avoid redundancy in your data.

So If you have gone through the above scenarios, I am pretty much sure that now you have understood why our arrays are not always a good fit and we can’t use them in every possible situation. Now, we have one more prominent question in our minds, If not array, then what.

Wait a minute, I’ll help you with that.

Java Collection Framework can you hear me ……….. Here comes our Saviour!!!!

Let’s take the help of our superhero aka the Java Collection Framework. Java Collection Framework contains a wide range of classes and interfaces which we can extend and implement to make our life easier. Moreover, it helps us to solve all the above-mentioned problems. Collection Framework can help you get rid of each of these problems. Don’t believe me? Wait let me show you something cool.

Let us make use of an ArrayList instead of an array. ArrayList is a part of the Java Collection framework. Let me give you a brief idea of what an ArrayList is. It is a dynamically sized array that can increase its size every time it’s about to get filled so if you are not sure about the size of your collection of data then it wouldn’t be a problem with an ArrayList.

As we have discussed in brief what an ArrayList is. So, now I believe that each one of you is able to figure out the differences here. It is helping us solve most of the traditional problems we were facing while working with arrays.

Now, let us see a demonstration on how an ArrayList should be used.

  • First of all, we don’t need to give any size to the ArrayList at the time of initialization. To use an ArrayList, first, we need to define an object so that we can use its behaviors that we want to. So that solves our first problem which was originating at the time of array initialization.
Fig 4. No size required at initialization
  • One down, Let’s see how it is helping us in resolving the second problem. So Arrays are static when it comes to size, right but ArrayLists are not. As we discussed that an ArrayList is a dynamically sized array, we can add as many elements in the ArrayList as we want. Due to this fine property of the ArrayList, we are able to resolve the second problem.
Fig 5. Adding extra elements not a problem
  • Tired of getting the alien text(hashcode), don’t worry ArrayList got your back. You can simply print the list using its reference. And I assure it wouldn’t give you any alien text this time(i.e, hashcode).
Fig 6. Printing the values instead of hashcode

So, this is how powerful the Java Collection Framework is, and by virtue of which you can handle any situation you want. There are many more classes that can help us make our life better and easier while programming in Java.

We can classify our Collection’s Framework on the basis of different criteria, let’s talk about them:-

  1. Collections with Ordering: Lists
  2. Collections for Uniqueness: Sets
  3. Collections for Pairs: Maps
  4. Collection for desired Ordering: Stacks, Queue, Priority Queue, Dequeue

Classification of Collection Framework

Fig 6. Classification of the Collection Framework

So we can already see how diverse and powerful our Java Collection is. We have a large number of classes for dealing with each kind of problem.

I will be publishing more blogs on the Collection Framework. There we will be discussing how different collection works, how they are implemented internally, and how they can be used in our real-life situations.

I hope you had a great time reading this blog, the same way I had while writing this.

Thank you

--

--

Shubham Rawat
Coding Blocks

Mentor and Product Engineer at Coding | Google Code In’ 18–19 Mentor JBoss(Redhat Middleware)| Open-Source Enthusiast| Contributor in JBoss(Redhat Middleware)