Java Compare Two Lists?

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 listOne = Arrays. AsList("milan","iga", "dingo","iga", "elpha","iga", "hafil","iga", "meat","iga", "neeta. Peeta","iga"); Collection listTwo = Arrays.

AsList("hafil", "iga", "binga", "mike", "dingo","dingo","dingo"); Collection similar = new HashSet( listOne ); Collection different = new HashSet(); 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?

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 listOne = Arrays.

AsList("milan","iga", "dingo","iga", "elpha","iga", "hafil","iga", "meat","iga", "neeta. Peeta","iga"); Collection listTwo = Arrays. AsList("hafil", "iga", "binga", "mike", "dingo","dingo","dingo"); Collection similar = new HashSet( listOne ); Collection different = new HashSet(); 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?

Oscar, My exact thought, but I was not sure if we could have modified the contents of listOne, but +1 anyways! – Anthony Forloney May 4 '10 at 0:53 You shouldn't use raw types. – polygenelubricants May 4 '10 at 0:56 @poygenelubricants what do you mean by raw types not generics?

Why not? – OscarRyz May 4 '10 at 0:58 Oscar, did you see my updated question? Does it support repeated values?

– user238384 May 4 '10 at 0:59 1 @polygenelubricants answer updated to handle duplicates and raw types. BTW, the ..future version of Java... is never going to happen. ;) – OscarRyz May 4 '10 at 1:13.

Because if it's the latter, then you can use, say, a java.util. HashSet and do this in expected linear time using the convenient retainAll. List list1 = Arrays.

AsList( "milan", "milan", "iga", "dingo", "milan" ); List list2 = Arrays. AsList( "hafil", "milan", "dingo", "meat" ); // intersection as set Set intersect = new HashSet(list1); intersect. RetainAll(list2); System.out.

Println(intersect.size()); // prints "2" System.out. Println(intersect); // prints "milan, dingo" // intersection/union as list List intersectList = new ArrayList(); intersectList. AddAll(list1); intersectList.

AddAll(list2); intersectList. RetainAll(intersect); System.out. Println(intersectList); // prints "milan, milan, dingo, milan, milan, dingo" // original lists are structurally unmodified System.out.

Println(list1); // prints "milan, milan, iga, dingo, milan" System.out. Println(list2); // prints "hafil, milan, dingo, meat.

Well I really don't know which data structure it should be. It has duplicates. Now you can see updated question – user238384 May 4 '10 at 0:57 Will it remove the repeated values from data set?

Coz I don't want to loss any value :( – user238384 May 4 '10 at 0:59 @agazerboy: I've tried to address both questions. Feel free to ask for more clarifications. – polygenelubricants May 4 '10 at 1:02 thanks poly.

I tried your program with duplicates for example in first list I added "iga" two times but still it return me 3 as an answer. While it should be 4 now. Coz list 1 has 4 similar values.

If I added one entry multiple time it should work. What do you say? Anyother data structure?

– user238384 May 4 '10 at 1:07 1 @agazerboy: try the latest edit. – polygenelubricants May 4 '10 at 1:23.

Intersection() gives you a collection containing common elements and the subtract() gives you all the uncommon ones. They should also take care of similar elements.

Assuming hash1 and hash2 List sames = whatever List diffs = whatever int count = 0; for( String key : hash1.keySet() ) { if( hash2. ContainsKey( key ) ) { sames. Add( key ); } else { diffs.

Add( key ); } } //sames.size() contains the number of similar elements.

He wants the list of identical keys, not how many keys are identical. I think. – Rosdi May 4 '10 at 0:42 Thanks stefan for your help.

Yeah Rosdi is correct and you as well. I need total number of similar values and similar values as well. – user238384 May 4 '10 at 0:44.

Compare them and create your own version from this, until you get what you need. Part of your question. You may use a java.util.

Collection and/or java.util. ArrayList for that. Removes all of this collection's elements that are also contained in the specified collection.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions