Alright
then, I am back, back to the world of blogging. It has been more than 3 months
since I wrote something. You may ask why there has been so much delay. Well, I
do not have any excuse for that (unless this can be considered as an excuse.
Well, I do not have any excuse itself is an excuse, isn’t it? It is a virtual
excuse, just like virtual memory of CPU. It is there as well as not there. Heh! Anyway, it seems I dragged this bracket on for too long, time to complete it.).
You can say that I haven’t come across anything extra ordinary enough to be
shared on blog. There you go. I have found an excuse for this, a physical
excuse (rather than a virtual one which we discussed earlier). You can term
this as Random Access Excuse (RAE) or Read Only Excuse (ROE) or excuse of any
other type (as long as it is not virtual excuse). It depends upon you.
It is worth mentioning that when you are passing the list of user defined objects to this method, you MUST override equals and hashcode methods in your class. The solution does not have any comparison logic. It relies on comparison logic of Set, and Set internally utilizes equals and hashcode methods to differentiate the objects. If you do not override these, all the objects will be marked as different even though all the attributes have same values (the reason being, if a class does not have its own equals and hashcode methods, same of Object class are utilized and they generate unique hashcode for each and every object. Hence, if hashcode is different, the objects are classified as different). More on equals and hashcode is explained here.
That’s that then. I have uploaded the source code with couple of comparisons. You can download it from here. Hope it helps you somewhere down the line. Also, a big thanks to Harshal Choksi for bringing up this scenario and providing the inputs whenever needed. And yes, I will try to be back quicker for the next blog.
~ Cheerio.
Enough
of bakchodi I guess. Let’s switch onto the main topic of the blog. In this
blog, we will discuss about comparison of two Java collections. Some of you
might say that comparing two collections in Java is a piece of cake. What did
you see extra ordinary enough in it to share? Well, I would say that we will
compare the collections the easiest way. We will not iterate any collection, we
will not even look into them, heck, we won’t even know the elements of
collections (i.e. we won’t know whether they will contain Integers, Strings or
any user defined objects)! It will be totally generic comparison. Does it sound
interesting now? I guess yes. Let’s move ahead then.
Have
you ever come across the use case in which, say, you are displaying a list of
20 items on UI, then the user edits some of the items (say 5) and clicks on
save button. Then, in the business layer (or service layer), you will have both
the lists (changed and unchanged one. (You can either store the unchanged list
on session or grab it back from database.)). From these 2 lists, you will have
to figure out which items got changed so that you can issue database update
queries on them. For this, you will have
to iterate the lists, compare the Items (possibly ItemDTO objects) and generate
the third list which contains only the changed ones. You know how painful it
can be to write the code for this? Don’t worry. Our solution is tailor made for
such problems.
The
solution needs 3 values from the consumer. They are described below:
- Source collection (i.e. the one coming from UI)
- Second collection (i.e. the one which the source collection needs to be compared against)
- Reference of result collection (the difference will be populated into this collection)
- Create two empty Sets i.e. first set and second set.
- Add all the elements of first collection into first set.
- Add all the elements of first collection into second set.
- Call retainAll method on second set. Pass second collection as an argument. E.g. secondSet.retainAll(secondCollection).This method retains the common elements into the collection which invoked it (i.e. intersection). More on this is described here. Hence, after the execution of this step, first set will contain all the elements of first collection and second set will contain the common (unchanged in our case) elements from both the collections.
- Call removeAll method on first set. Pass second set as an argument. E.g. firstSet.removeAll(secondSet). This step will remove all the common elements from first set. In our case, all the Items which are unchanged from UI will be removed. More on removeAll is described here.
- Populate the third collection with the elements of first set.
- Voila! We are done. And this is not actually a step.
It is worth mentioning that when you are passing the list of user defined objects to this method, you MUST override equals and hashcode methods in your class. The solution does not have any comparison logic. It relies on comparison logic of Set, and Set internally utilizes equals and hashcode methods to differentiate the objects. If you do not override these, all the objects will be marked as different even though all the attributes have same values (the reason being, if a class does not have its own equals and hashcode methods, same of Object class are utilized and they generate unique hashcode for each and every object. Hence, if hashcode is different, the objects are classified as different). More on equals and hashcode is explained here.
That’s that then. I have uploaded the source code with couple of comparisons. You can download it from here. Hope it helps you somewhere down the line. Also, a big thanks to Harshal Choksi for bringing up this scenario and providing the inputs whenever needed. And yes, I will try to be back quicker for the next blog.
~ Cheerio.