• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Find matches in two collections

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:


 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic