Java中的 AbstractSequentialList equals() 方法和示例
Java.util.AbstractSequentialList类的equals()方法用于检查这个AbstractSequentialList 对象是否等于作为参数传递的对象。此方法返回一个布尔值,说明相同。
句法:
public boolean equals(Object o)
参数:此方法将对象 o作为参数,以便与此列表比较是否相等。
返回值:如果指定的对象等于此列表,则此方法返回true 。
下面是说明equals()方法的示例。
示例 1:
// Java program to demonstrate equals()
// method of AbstractSequentialList
import java.util.*;
public class GFG {
public static void main(String[] argv)
{
// Creating object of AbstractSequentialList
AbstractSequentialList
arrlist1 = new LinkedList();
// Populating arrlist1
arrlist1.add("A");
arrlist1.add("B");
arrlist1.add("C");
arrlist1.add("D");
arrlist1.add("E");
// print arrlist1
System.out.println("First AbstractSequentialListlist: "
+ arrlist1);
// Creating another object of AbstractSequentialList
AbstractSequentialList
arrlist2 = new LinkedList();
// Populating arrlist2
arrlist2.add("A");
arrlist2.add("B");
arrlist2.add("C");
arrlist2.add("D");
arrlist2.add("E");
// print arrlist2
System.out.println("Second AbstractSequentialList: "
+ arrlist2);
// comparing first AbstractSequentialList to another
// using equals() method
boolean value
= arrlist1.equals(arrlist2);
// print the value
System.out.println("Are both list equal: "
+ value);
}
}
输出:
First AbstractSequentialListlist: [A, B, C, D, E]
Second AbstractSequentialList: [A, B, C, D, E]
Are both list equal: true
示例 2:
// Java program to demonstrate equals()
// method of AbstractSequentialList
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
{
// Creating object of AbstractSequentialList
AbstractSequentialList
arrlist1 = new LinkedList();
// Populating arrlist1
arrlist1.add(10);
arrlist1.add(20);
arrlist1.add(30);
arrlist1.add(40);
arrlist1.add(50);
// print arrlist1
System.out.println("First AbstractSequentialListlist: "
+ arrlist1);
// Creating another object of AbstractSequentialList
AbstractSequentialList
arrlist2 = new LinkedList();
// Populating arrlist2
arrlist2.add(10);
arrlist2.add(20);
arrlist2.add(30);
// print arrlist2
System.out.println("Second AbstractSequentialList: "
+ arrlist2);
// comparing first AbstractSequentialList to another
// using equals() method
boolean value = arrlist1.equals(arrlist2);
// print the value
System.out.println("Are both list equal: "
+ value);
}
}
输出:
First AbstractSequentialListlist: [10, 20, 30, 40, 50]
Second AbstractSequentialList: [10, 20, 30]
Are both list equal: false