• Post Reply Bookmark Topic Watch Topic
  • New Topic

Between Two arrayLists how do I find which elements they have in common?  RSS feed

 
Raed Tabani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two ArrayLists and I want to compare them for common elements, and based on the result I want to update the first Arraylist to only have these elements. sort of like the opposite of RemoveAll() which removes elements in common and keep the ones that are unique. so far I thought of using for loop and .contains() in case it was fault,element not present, remove from list. but I was wondering
in what other ways, perhaps APIs i can use to do that?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use Sets for that. Put the elements of each array into a set and get the disjunction of these two sets. From there, you put the remaining elements back into a List.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's more info about the Java Set interface: https://docs.oracle.com/javase/tutorial/collections/interfaces/set.html
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Apache Commons CollectionUtils also has a way to calculate the disjunction of two collections: https://commons.apache.org/proper/commons-collections/javadocs/api-3.2.1/org/apache/commons/collections/CollectionUtils.html
 
Liutauras Vilda
Sheriff
Posts: 4923
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raed,

Usually quite difficult to give an answer to very abstract question. Also, usually there are lots of different ways to achieve the same task.
Did you research what you can do with ArrayList class? JavaSE 8 API
Have you tried to google it? results
Did you come up with something concrete already, for example by using your above mentioned technique? Or you just looking the point where you could start?
 
Raed Tabani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:You could use Sets for that. Put the elements of each array into a set and get the disjunction of these two sets. From there, you put the remaining elements back into a List.

you are right, I did a little research on bitset and I think the disjunction you were refereing to is the add(), so here's an implementation and it works perfect
 
Raed Tabani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Hi Raed,

Usually quite difficult to give an answer to very abstract question. Also, usually there are lots of different ways to achieve the same task.
Did you research what you can do with ArrayList class? JavaSE 8 API
Have you tried to google it? results
Did you come up with something concrete already, for example by using your above mentioned technique? Or you just looking the point where you could start?


hi Liutaurus,
I'm sorry about that, I guess to be more specific I meant to ask how can I do it better and weather or not some APIs in ArrayList class exist to achieve that. and apparently there is!
I just found the retainAll() and it did the job perfectly


this was the old way


 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's good that you found a solution despite my misreading your requirements. I was thinking that you wanted something closer to the difference between two sets. Since you wanted to "update the first Arraylist to only have (the elements it has in common with the second Arraylist)" then you needed to do:

This modifies the first collection so that it only contains the elements it has in common with the second collection, essentially leaving it as the intersection of the two original sets.
 
Raed Tabani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lol I think it's funny, you misread my question and I misread your answer but somehow got to where I wanted actually I'm glad that you did, this was my first time ever using a bitset and finding about and() and andNot() which I think will be useful to me in the future. and you are absolutely right, this would be an inefficient way to go about doing what I wanted especially with the retainAll().
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will probably find that your method with nested loops runs in quadratic time and the retainAll method will run in nlogn time. The API does not specify that, however.
You can maintain the original List like this:-… because retainAll returns a boolean.
 
Raed Tabani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi campbell,
actually the program doesn't require the list to be maintained as with each loop itertation,not shown in this code, it gets refilled. but thanks anyways, I'll keep that in mind
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!