當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java AbstractList equals()用法及代碼示例


java.util.AbstractList 類的 equals() 方法用於比較指定對象與此列表是否相等。當且僅當指定對象也是一個列表時返回 true,兩個列表具有相同的大小,並且兩個列表中所有對應的元素對都相等。 (如果 (e1==null ? e2==null:e1.equals(e2)) 兩個元素 e1 和 e2 相等。)換句話說,如果兩個列表以相同的順序包含相同的元素,則它們被定義為相等.

用法:

public boolean equals(Object o)

參數:此方法將對象 o 作為參數來比較與此列表是否相等。

Returns Value:如果指定的對象等於此列表,則此方法返回 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<String>
            AbstractList<String>
                arrlist1 = new ArrayList<String>();
  
            // 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<String>
            AbstractList<String>
                arrlist2 = new ArrayList<String>();
  
            // 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<Integer>
            AbstractList<Integer>
                arrlist1 = new ArrayList<Integer>();
  
            // 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<Integer>
            AbstractList<Integer>
                arrlist2 = new ArrayList<Integer>();
  
            // 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




相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 AbstractList equals() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。