Java program to remove one array from another array

To delete an array from another array in Java, we will use the removeAll() method. If we call the removeAll() function from array2 and array1 as parameters, this will remove all elements of array1 from array2.

Syntax:

public boolean removeAll(Collection c)

Parameters: 

Returns Value: This method returns true if this list changed as a result of the call.

Example 1:

// Java program to Remove One Array From Another Array

import java.util.ArrayList;
import java.util.List;

class GFG {
	public static void main(String[] args)
	{

		// creating first array
		List firstList = new ArrayList<>();

		// creating second array
		List secondList = new ArrayList<>();

		// adding elements in first array
		firstList.add(100);
		firstList.add(200);
		firstList.add(300);
		firstList.add(400);
		firstList.add(500);
		firstList.add(600);

		// adding elements in second array
		secondList.add(300);
		secondList.add(500);
		
		// displaying element of first array
		System.out.println("elements in first array "
						+ firstList);
		
		// displaying element of second array
		System.out.println("elements in second array "
						+ secondList);
		
		// removing elements from firstarray
		firstList.removeAll(secondList);

		// displaying elements of first array
		// after removing elements of second array
		// from first array
		System.out.println("first array after removing second array from first array\n"
			+ firstList);
	}
}

Output

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]

Example 2: In this example we are using a custom class instead of a Java shell class, but to use a custom class we need to override the equals() and hashCode() methods.

 

// Java program to Remove One Array From Another Array

import java.io.*;
import java.util.*;

class friendsDetail {

	// class field
	private String name;
	private String nickName;

	// parameterised constructor
	public friendsDetail(String name, String nickName)
	{
		this.name = name;
		this.nickName = nickName;
	}

	// getter for name
	public String getName() { return name; }

	// setter for name
	public void setName(String name) { this.name = name; }

	// getter for nickname
	public String getnickName() { return nickName; }

	// setter for nickname
	public void setNickName(int id)
	{
		this.nickName = nickName;
	}

	@Override public boolean equals(Object o)
	{

		if (this == o)
			return true;

		if (!(o instanceof friendsDetail))
			return false;

		friendsDetail that = (friendsDetail)o;

		return Objects.equals(getName(), that.getName())
			&& Objects.equals(nickName, that.nickName);
	}

	@Override public int hashCode()
	{
		return Objects.hash(getName(), nickName);
	}

	// overriding toString method
	public String toString()
	{
		// return super.toString();
		return "(" + this.getName() + ":"
						+ this.getnickName() + ")";
	}
}
public class GFG {
	public static void main(String[] args)
	{
		ArrayList firstArrayList = new ArrayList<>();

		System.out.println("Our First ArrayList\n");

		// adding elements to first ArrayList
		firstArrayList.add(new friendsDetail("Amit", "Ghulla"));
		firstArrayList.add(new friendsDetail("Yashdeep", "Dopa"));
		firstArrayList.add(new friendsDetail("Jyoti", "Kauwa"));
		firstArrayList.add(new friendsDetail("Suraj", "Bhindi"));
		firstArrayList.add(new friendsDetail("Himanshu", "Lalten"));
		firstArrayList.add(new friendsDetail("Sarthak", "Nagin"));
		firstArrayList.add(new friendsDetail("Tsering", "Battak"));
		firstArrayList.add(new friendsDetail("Abhishek", "Liquid"));

		// Displaying output of first array using enhanced
		// for loop
		for (friendsDetail friend : firstArrayList) {
			System.out.println(friend);
		}

		ArrayList secondArrayList = new ArrayList<>();

		System.out.println("\nOur Second ArrayList\n");
		
		// adding elements to second ArrayList.
		secondArrayList.add(new friendsDetail("Amit", "Ghulla"));
		secondArrayList.add(new friendsDetail("Jyoti", "Kauwa"));
		secondArrayList.add(new friendsDetail("Himanshu", "Lalten"));
		secondArrayList.add(new friendsDetail("Abhishek", "Liquid"));

		// Displaying output of original array using
		// enhanced for loop
		for (friendsDetail friend : secondArrayList) {
			System.out.println(friend);
		}

		// removing second array elements from first array
		firstArrayList.removeAll(secondArrayList);

		System.out.println(
		"\nFirst array after removing second array from first array\n");

		// Displaying first array after removing second
		// array elements from first array
		for (friendsDetail friend : firstArrayList) {
			System.out.println(friend);
		}
	}
}

Output

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)

 

Submit Your Programming Assignment Details