Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Compare Two Lists

I have two lists ( not java lists, you can say two columns)

For example

**List 1**            **Lists 2**   milan                 hafil   dingo                 iga   iga                   dingo   elpha                 binga   hafil                 mike   meat                  dingo   milan   elpha   meat   iga                      neeta.peeta     

I'd like a method that returns how many elements are same. For this example it should be 3 and it should return me similar values of both list and different values too.

Should I use hashmap if yes then what method to get my result?

Please help

P.S: It is not a school assignment :) So if you just guide me it will be enough

like image 387
user238384 Avatar asked May 04 '10 00:05

user238384


People also ask

How do I compare two lists in Java?

Java provides a method for comparing two Array List. The ArrayList. equals() is the method used for comparing two Array List. It compares the Array lists as, both Array lists should have the same size, and all corresponding pairs of elements in the two Array lists are equal.

How do you compare a field between two lists of objects?

Java equals() method This method accepts an object to be compared for equality with the list. It returns true if the specified object is equal to the list, else returns false. In the following example, we have create two ArrayList firstList and secondList. Comparing both list by using equals() method, it returns true.


1 Answers

EDIT

Here are two versions. One using ArrayList and other using HashSet

Compare them and create your own version from this, until you get what you need.

This should be enough to cover the:

P.S: It is not a school assignment :) So if you just guide me it will be enough

part of your question.

continuing with the original answer:

You may use a java.util.Collection and/or java.util.ArrayList for that.

The retainAll method does the following:

Retains only the elements in this collection that are contained in the specified collection

see this sample:

import java.util.Collection; import java.util.ArrayList; import java.util.Arrays;  public class Repeated {     public static void main( String  [] args ) {         Collection listOne = new ArrayList(Arrays.asList("milan","dingo", "elpha", "hafil", "meat", "iga", "neeta.peeta"));         Collection listTwo = new ArrayList(Arrays.asList("hafil", "iga", "binga", "mike", "dingo"));          listOne.retainAll( listTwo );         System.out.println( listOne );     } } 

EDIT

For the second part ( similar values ) you may use the removeAll method:

Removes all of this collection's elements that are also contained in the specified collection.

This second version gives you also the similar values and handles repeated ( by discarding them).

This time the Collection could be a Set instead of a List ( the difference is, the Set doesn't allow repeated values )

import java.util.Collection; import java.util.HashSet; import java.util.Arrays;  class Repeated {       public static void main( String  [] args ) {            Collection<String> listOne = Arrays.asList("milan","iga",                                                     "dingo","iga",                                                     "elpha","iga",                                                     "hafil","iga",                                                     "meat","iga",                                                      "neeta.peeta","iga");            Collection<String> listTwo = Arrays.asList("hafil",                                                      "iga",                                                      "binga",                                                       "mike",                                                       "dingo","dingo","dingo");            Collection<String> similar = new HashSet<String>( listOne );           Collection<String> different = new HashSet<String>();           different.addAll( listOne );           different.addAll( listTwo );            similar.retainAll( listTwo );           different.removeAll( similar );            System.out.printf("One:%s%nTwo:%s%nSimilar:%s%nDifferent:%s%n", listOne, listTwo, similar, different);       } } 

Output:

$ java Repeated One:[milan, iga, dingo, iga, elpha, iga, hafil, iga, meat, iga, neeta.peeta, iga]  Two:[hafil, iga, binga, mike, dingo, dingo, dingo]  Similar:[dingo, iga, hafil]  Different:[mike, binga, milan, meat, elpha, neeta.peeta] 

If it doesn't do exactly what you need, it gives you a good start so you can handle from here.

Question for the reader: How would you include all the repeated values?

like image 195
OscarRyz Avatar answered Sep 18 '22 17:09

OscarRyz