Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Find matches in two collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Find matches in two collections" Watch "Find matches in two collections" New topic
Author

Find matches in two collections

Emilio Heredia
Greenhorn

Joined: Dec 10, 2009
Posts: 4
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

Joined: Sep 12, 2007
Posts: 300
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

Joined: Dec 10, 2009
Posts: 4
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

Joined: Dec 10, 2009
Posts: 4
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

Joined: Sep 12, 2007
Posts: 300
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

Joined: Dec 10, 2009
Posts: 4
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

Joined: Sep 12, 2007
Posts: 300
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

Joined: Oct 27, 2005
Posts: 19649
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
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:


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Find matches in two collections
 
Similar Threads
Linked List equals method
Difference between List<String> list1 = new ArrayList(); and List<String> list2 = new ArrayList<Stri
Accessing elements from an arraylist
Sorting-Java Comparator-predefined list
Collection