ArrayList.equals(Object) in Java

ArrayList.equals(Object): This method is available in java.util.ArrayList class of Java.

Syntax:

boolean java.util.ArrayList.equals(Object o)

This method takes one argument. This method compares the specified object with this list for equality.

Note: Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal.

Parameters: One parameter is required for this method.

o: the object to be compared for equality with this list.

Returns: true if the specified object is equal to this list.

Exceptions: NA

Approach

Java

import java.util.ArrayList;

public class ArrayListequals {
    public static void main(String[] args) {
        ArrayList<Integer> arr = new ArrayList<>();
        ArrayList<Integer> arr1 = new ArrayList<>();
        arr.add(1);
        arr.add(2);
        arr.add(3);

        arr1.add(1);
        arr1.add(2);
        arr1.add(3);
        System.out.println(arr.equals(arr1));
    }
}

Output:

true


No comments:

Post a Comment