Remove One Array From Another Array in Java
For removing one array from another array in java we will use the removeAll() method. This will remove all the elements of the array1 from array2 if we call removeAll() function from array2 and array1 as a parameter.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
public boolean removeAll(Collection c)
Parameters: This method takes collection c as a parameter containing elements to be removed from this list.
Returns Value: This method returns true if this list changed as a result of the call.
elements in first array [100, 200, 300, 400, 500, 600] elements in second array [300, 500] first array after removing second array from first array [100, 200, 400, 600]
In this example instead of using wrapper classes of java, we will use user-defined class but for using user-defined class we need to override equals() and hashCode() method.
Our First ArrayList (Amit:Ghulla) (Yashdeep:Dopa) (Jyoti:Kauwa) (Suraj:Bhindi) (Himanshu:Lalten) (Sarthak:Nagin) (Tsering:Battak) (Abhishek:Liquid) Our Second ArrayList (Amit:Ghulla) (Jyoti:Kauwa) (Himanshu:Lalten) (Abhishek:Liquid) First array after removing second array from first array (Yashdeep:Dopa) (Suraj:Bhindi) (Sarthak:Nagin) (Tsering:Battak)