Reputation: 1047
I have one problem, I need to compare two arrays and I want to get unmatched values.
Example:
List<String> Array1=new ArrayList<String>();
List<String> Array2=new ArrayList<String>();
List<String> Array3=new ArrayList<String>();
Array1.add("1");
Array1.add("23");
Array1.add("1211");
Array1.add("12232");
Array1.add("231");
Array1.add("2231");
Array2.add("1");
Array2.add("23");
Array2.add("231");
Array2.add("2231");
// Array3 values are 1211 12232
Is this possible?
Upvotes: 0
Views: 2932
Reputation: 8900
use Set ( It will remove duplicates too )
Set<String> set1 = new HashSet(array1);
Set<String> set2 = new HashSet(array2);
set1.removeAll(set2);
Upvotes: 3
Reputation: 19185
You need to use removeAll
.
List<String> array3 = new ArrayList<String>(array1);//Create copy of array 1
array3.removeAll(array2);//Remove common elements
Upvotes: 3
Reputation: 8518
You'll have to do it yourself, but it's not terribly hard. One possible solution:
Collections.sort( Array2 ); // Don't need this one if Array2 is already sorted.
for( String s : Array1 ) {
if ( Collections.binarySearch( Array2, s ) < 0 )
Array3.add( s );
}
Upvotes: 0