Reputation: 71
hi am storing the results in array list which are obtained by querying different database in java. i need to compare this array list to get the common rows
List name = new ArrayList();
name.add(Spreadsheets.getString("Name"));
List namedb = new ArrayList();
namedb .add(rs.getString("eName"))
in c++ we use strcmp to compare. but in java how do i compare this arraylist and it should return common rows. or how do i apply binary search algorithm to search these to arraylist?
Upvotes: 0
Views: 135
Reputation: 3512
Use the List's retainAll
method.
List<String> names = new ArrayList<String>();
List<String> namesDB = new ArrayList<String>();
// Get some names ??
names.add(Spreadsheets.getString("Name"));
boolean hasCommonNames = namesDB.retainAll( names );
// The namesDB will only contain the common names.
if( hasCommonNames ) {
// Do something with the common names
}
Upvotes: 1
Reputation: 31627
ArrayList<Integer> a3 = new ArrayList<Integer>();
for (String a : a1)
a3.add(a2.contains(a) ? 1 : 0);
Now check content of a3. If all is 1, two list are same...
Upvotes: 0
Reputation: 951
You can use a HashMap to store the first set of results , and then iterate through the second list , during iteration check if that value exists in the HashMap , if it exists , push it into another List C , at the end of the iteration , List C will have the common elements
Upvotes: 0