JAVA - How To Remove All Objects In An Arraylist That Exist In Another Arraylist

JAVA - How To Remove All Objects In An Arraylist If Exist In Another Arraylist

_________________________________________________________________________

In this java Collection Tutorial we will see How To Delete All Elements from ArrayList
If Elements Exist In Another ArrayList In Java NetBeans .

Source Code:

package javadb_001;
import java.util.ArrayList;
import java.util.List;
public class Learn {
    
public static void main(String[] args){
    String[] str1 = {"C#","JAVA","Android","ASP.NET","J2EE","PHP"};
 List<String> list = new ArrayList<String>(); 
 //fill the list
 for(String s1 : str1){
     list.add(s1);
     }
     String[] str2 = {"C","C++","JAVA","DELPHY","HTML","CSS","C#"};
     List<String> list2 = new ArrayList<String>();
     
     //fill the list2
     for(String s2 : str2){
         list2.add(s2);
     }
     
    //display the list2 Objects before removing existing objects
     for(String s : list2){
         System.out.println(s);
     }
      System.out.println("____________________");
      
     for(int i = 0; i < list.size(); i++){
         for(int j = 0; j < list2.size(); j++)
         {
             if(list.get(i).equals(list2.get(j))){
                 list2.remove(j);
             }
         }
     }
     
     //display the list2 Objects after removing existing objects
     for(String s : list2){
         System.out.println(s);
     }
}
}



Share this

Related Posts

Previous
Next Post »