Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# To compare 2 list and obtain the unmatched values from the list

kannan coding
Greenhorn
Posts: 12
I have 2 list.

for example, list1 has [2,14]
list2 has [2,8,34,7,14,6]

Now need the output as follows [8,34,7,6]

i.e to remove the values of list1 from list2.
Kindly help me to solve this.

kannan coding
Greenhorn
Posts: 12
I get the solutions as follows:
package com;

import java.util.*;
public class sample2 {
public static void main(String[] args){
ArrayList ar1 = new ArrayList();
ArrayList ar2 = new ArrayList();
ArrayList ar3 = new ArrayList();
System.out.println("List 1: " + ar1);
System.out.println("List 2: " + ar2);
System.out.println("List 3: " + ar3);

Iterator itr1 = ar1.iterator();
Iterator itr2 = ar2.iterator();

while(itr1.hasNext()){
String id1 = (String)itr1.next();
while(itr2.hasNext()){
String id2 = (String)itr2.next();

if(id1!=null && id2!=null){
if(!id1.equals(id2)){
ar3.remove(id1);
}
}
}
itr2 = ar2.iterator();
}
System.out.println("Final list:" + ar3);
}
}

Kindly Provide me a better solution.

David Newton
Author
Rancher
Posts: 12617
This has nothing to do with Struts, please ask this question in the "Java n General - Beginner" forum.

Be aware that this sounds a lot like a homework assignment, which may provoke negative reactions.