📌  相关文章
📜  Java中的 AbstractList equals() 方法及示例

📅  最后修改于: 2022-05-13 01:55:34.675000             🧑  作者: Mango

Java中的 AbstractList equals() 方法及示例

Java.util.AbstractList类的equals()方法用于比较指定对象与该列表是否相等。当且仅当指定的对象也是一个列表时返回 true,两个列表具有相同的大小,并且两个列表中所有对应的元素对都相等。 (如果 (e1==null ? e2==null : e1.equals(e2)) 两个元素 e1 和 e2 相等。)换句话说,如果两个列表以相同的顺序包含相同的元素,则它们被定义为相等.

句法:

public boolean equals(Object o)

参数:此方法将对象 o作为参数,以便与此列表比较是否相等。

返回值:如果指定的对象等于此列表,则此方法返回true

下面是说明equals()方法的示例。

示例 1:

// Java program to demonstrate
// equals() method
// for String value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // Creating object of AbstractList
            AbstractList
                arrlist1 = new ArrayList();
  
            // Populating arrlist1
            arrlist1.add("A");
            arrlist1.add("B");
            arrlist1.add("C");
            arrlist1.add("D");
            arrlist1.add("E");
  
            // print arrlist1
            System.out.println("First ArrayListlist : "
                               + arrlist1);
  
            // Creating another object of AbstractList
            AbstractList
                arrlist2 = new ArrayList();
  
            // Populating arrlist2
            arrlist2.add("A");
            arrlist2.add("B");
            arrlist2.add("C");
            arrlist2.add("D");
            arrlist2.add("E");
  
            // print arrlist2
            System.out.println("Second ArrayList : "
                               + arrlist2);
  
            // comparing first ArrayList to another
            // using equals() method
            boolean value = arrlist1.equals(arrlist2);
  
            // print the value
            System.out.println("Are both list equal : "
                               + value);
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
First ArrayListlist : [A, B, C, D, E]
Second ArrayList : [A, B, C, D, E]
Are both list equal : true

示例 2:

// Java program to demonstrate
// equals() method
// for Integer value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // Creating object of AbstractList
            AbstractList
                arrlist1 = new ArrayList();
  
            // Populating arrlist1
            arrlist1.add(10);
            arrlist1.add(20);
            arrlist1.add(30);
            arrlist1.add(40);
            arrlist1.add(50);
  
            // print arrlist1
            System.out.println("First ArrayListlist : "
                               + arrlist1);
  
            // Creating another object of AbstractList
            AbstractList
                arrlist2 = new ArrayList();
  
            // Populating arrlist2
            arrlist2.add(10);
            arrlist2.add(20);
            arrlist2.add(30);
  
            // print arrlist2
            System.out.println("Second ArrayList : "
                               + arrlist2);
  
            // comparing first ArrayList to another
            // using equals() method
            boolean value = arrlist1.equals(arrlist2);
  
            // print the value
            System.out.println("Are both list equal : "
                               + value);
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
First ArrayListlist : [10, 20, 30, 40, 50]
Second ArrayList : [10, 20, 30]
Are both list equal : false