Hello Friends! This week was busier than I thought and sorry for not posting any post this Wednesday. And there is a news for you all, I released my own website this week and there it is. Make sure you visit my site and provide feedbacks/suggestions on how can I make it better.
This week was very much different and I learned a lot of things this week an today’s subject of this blog is one of those things. After going to all these things I am back to my Java mood.
What a collection is?
You might think what this collection thing is. But let me tell you, this collection concept in Java is no different than any kind of collection that probably you used to do as a hobby when you were a kid. Be it Stamps, Marbles, Colorful Stones, Rare coins etc. all these were the objects that made your collection a collection. And each object of your collection had something related to all other objects. Their properties, size, type etc. used to match to each other.
This is the analogy which Java follows in its collection concept. As you can see I have underlined few words in about paragraph. These are the terminology which are applied in Java as well as they are used in your daily life.
You should go to this link if you want a thorough documentation on Collections in Java but if you have already read this concept then I am going to make it stick to your mind through this blog post. Here I will be using different terms having an equivalent Java term. But let me remind you, my primary objective here is to make these concepts easy to grasp for you and not just typing the stuff that I read somewhere else as it is. Instead I will provide you examples that I have used to make this concept stick to my memory.
Collection: Simply, Collection is nothing but a group of similar kind of Objects(Just your collection of coins). It is also called a Container sometimes.
In your collection of coins you may have some particular order that you follow to collect your coins, you may want to search for a coin, you may want all coins in your collection to be different or you may have a sorting mechanism to sort your coins in the increasing order of there age. Similarly Java has provided you with some methods to automatize such tasks in your collection in Java.
You may have seen the picture below somewhere. If not then do visit this Java documentation to have a understanding of Collections in Java and get back here to make collections stick to your brain.Depending on the desired features Collections are of many types which I will mention in a bit.
This picture above shows the hierarchy that Collections in Java follows. As you can see the above picture is divided in two parts. The smaller part is having Map and SortedMap as its members. We will discuss them later.
Types of Collection:
Just like your coin collection sitting in your closet, Collections in Java can also have several types, but remember that all these specific kind of collections are a Collection first i.e. all these following collections extends Collection interface.
- Queue: Lets say when you started collecting coins, you started doing so in by collecting the oldest coins first and you collect coins only in increasing order of their manufacturing date or we can say in decreasing order of their age. Now suppose you want to give away your collection to your younger brother and you do this in a manner that you give away the oldest coin first and then second oldest coin second and so. This particular fashion of your kindness is called as FIFO(First In First Out) manner. Queue is a very basic data structure used in programming and you can get its features in your programs by using Queue collection from Java Collection framework.
- List: A coin collection without any specifications comes under a List kind of Collection in Java. In other words, you can add coins to your collection, check if a specific coin is already present in your collection or not, add two exactly same kind of coins to your collection, remove any coin or removeAll coins from your collection etc. These are the exact behavior that you can achieve in your Java implementations.
- Set: A set collection of coin is a kind of coin collection where you have strictly prohibited the addition of duplicate coins. Duplicate in the sense of preexisting and not fake. So when you add a old 5 Rupee coin in your collection but the same coin is already present in the collection then this newly added coin gets vanished into thin air from your collection. In other words, the newly added coin overwrites that old 5 Rupee coin. So in a Set collection, duplicate items are strongly prohibited.
- Sorted Set: Suppose you have a good habit of arranging your coins in increasing order of there age as the come to you. This good habit of yours save a lot of time of yours whenever it comes to do some kind of operation on your coin collection like searching for a particular coin, adding a coin in middle of preexisting collection or even removing a coin from the collection. Java has provided the Sorted Set interface for your help in these kind of operations. Sorted Set extends Set interface.
- Deque: Deque (pronounced as ‘Deck’) is nothing but a double-ended-queue. Think of it as in your coin collection you can add or delete or modify your coins from any of the ends. You can always check which coin is present at starting end of your collection or at other end of your collection.
Now I am going to tell you the reason why Map is not in the same block as other Collection interfaces in above picture. Map are a bit different than formal collections in Java.
A Map is an object that maps keys to values i.e. all the objects present in a Map collection have a key-value pair, also called as an Entry. A Map contain at least one Entry. And just like Set collection, Map also doesn’t allow duplicate entries.
Suppose, in your coin collection, whenever you collect a coin along with it you collect a document of its information like its age, origin, manufacturing date etc. as well. Now, Think of the coins in your collection as a key and the respective document as a value of Key-Value pair. So a coin and its document collectively make up an Entry of the collection. The type of both Key and Value may or may not be different.
And just like a single coin cannot have two different documents of authenticity, similarly a key cannot have two values at a given time.
You can do all sorts of operations on Map which you can do on any other collection of Collection framework in Java like searching for an key-value pair(entry), deleting an entry from the collection, updating an entry and so on.
- Sorted Map: Sorted Map is just a Map in a sorted order. In Java, generally this particular sorting order is ascending order.
I hope you will find this blog post helpful and informative. If you think I have missed out on something here then do comment down below to make me remind of that. Please provide your feedbacks and/or requests. Do follow my Blog by clicking the follow button below and entering your E-mail address there so that you don’t miss any update of my Blog. Keep visiting 🙂