A Beginners Guide to Understand Collections in JAVA
Hello friends, in this article I’ll try to make collections very easy for you to understand. This is one of the easiest and yet misunderstood concept for the beginners. It is pretty easy to get started with collections but to understand the use of collections takes some experience and understanding of the real world problem modelling. What I mean to say here is that, beginners often misunderstood the use of collection where it is most important. That is a common scenario because it is hard for them to model real world problems into objects and collections rather than using an old procedural style.
As this article specifically belongs to the beginners, so I would start the way every other article or a beginner is briefed with and i.e, from the scratch and the definition of collections. It is Okay to leave this article if you think you know the theory/syntax and move directly to the other part of it where we will take on a real world problem and try to solve it using collections. I believe in the philosophy of doing things practically rather than theory. So, that is what we will do. Let’s get started…
What are Collections in JAVA?
Collections in JAVA is simply a framework that provides an architecture to store and retrieve a group of objects. It is a ready made data structure that stores group of objects and provides various different ways to manipulate it. It performs all the operations that you usually perform on a set of data, such as searching, sorting, insertion, deletion etc… It makes your task very easy and the best part – You don’t need to know the working behind it. Simply, learn to call the different methods and all hard part will be taken care by the framework itself.
To further elaborate, JAVA Collection simply means a single unit of objects. It also provides many different interfaces and classes to structure the data as needed. Most widely used interfaces are –
It also provides many useful classes which implements one of these frameworks and provides the required functionality and behaviour. Some of the most widely used classes in JAVA Collection framework are –
- LinkedHashSet, etc…
Hierarchy of Collection Framework
You will find all the classes for collection framework in java.util package. This is how the interfaces and classes are linked with each other.
Enough of the theory, as I said, I believe in getting hands dirty into the task that we are alloted with. It is time to get your hands dirty in code. Let’s learn the few basics that will help you in the second part of this article where we will model a real world problem.
How to use Collection Framework?
Glad you asked, as I said it is really very easy to get started with JAVA Collection. So, let’s quickly delve in…
Follow the CollectionExample.java class below…
For the time being, just go through the code and simply uncomment each line in the main method one-by-one and change the printList argument depending on the uncommented code. For ex- if you uncomment the
c.setExample(); then change the argument of the
cc.printList(c.set); . It will tell printList method about the type of which is being thrown at him and it will decide depending on the same. The above example was of the Non-generics collection types. In Non-generics, you can insert any type of object. It doesn’t restrict you before adding anything to it. However, this is not the right way to do. Eventually, you will end up writing lots of instanceof check in the code which is pretty ugly and most probably face lots of ClassCastException errors. Just to avoid these things and to make java more compiler friendly GENERICS were introduced. Generics lets you define the type of object that it will store beforehand. So, if you want a list of names, then you can provide the type as String. This will restrict the entry of any unwanted object type which is not an instance of the class String. That way your code will look cleaner and much manageable. It will become easier to manage such a code in long runs. I will extend the above class file to demonstrate the use of Generics in collection…
This article was intended to educate you on the use of collections framework in JAVA and how to get started with it. If you understood the concepts presented in this article then it’s time for a practical use of it. In the next article, you will learn the use of collections in real world applications. How to model application and use collections to make our jobs easier. Follow the link below,