• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Find matches in two collections

 
Emilio Heredia
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i find matches comparing two collections ?

For example i have ...



list1 have "dog" and in list2 also have "dog", how can i create a method who finds that ?

I have a very large collection, of 50 thousands Strings and i want to compare it with other collection to find matches, is there a way to do that ?

thanks in advance
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want to get common values in the two collections in the form of another list?

I just want to know the output you want to achieve.
 
Emilio Heredia
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Patricia Samuel wrote:You want to get common values in the two collections in the form of another list?

I just want to know the output you want to achieve.


yes exactly that !

i want to find the matching values between the two collections i have and put that matches in a third collection
 
Emilio Heredia
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i found a way to do it !

however what i did is probably a waste of cpu, if you can help me with a better code i will truly appreciate it ...

 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it can be done using loops. You can check which collection has lesser size and iterate through that collection

something like this



 
Emilio Heredia
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Patricia, but i will stay simple for now ...

I think everybody could affort a good computer nowadays, even my old laptop only takes 3 seconds to compare a collection of 50 thousands word with another one of 80 thousands
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Emilio ,

Just curious to know whether you have checked the performance of the logic we both implemented. Does the latter one perform better?


Thanks
Patricia.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd use the bulk methods of Collection instead. You create a copy of one list, then remove everything from that copy that is not in the other one. The remaining elements then are part of both.
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you do not need to use a List, then you could perhaps try Sets for your original collections. A HashSet allows finding elements in constant time. Then you can try something like:


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic