Java中的 CopyOnWriteArraySet equals() 方法
CopyOnWriteArraySet的equals(Object O)方法将指定的对象与此集合进行比较是否相等。如果指定对象与此对象相同,或者它也是一个 Set 并且指定集合上的迭代器返回的元素与此集合上的迭代器返回的元素相同,则返回 true。更正式地说,如果两个迭代器返回相同数量的元素,则认为这两个迭代器返回相同的元素,并且对于迭代器在指定集合上返回的每个元素 e1,迭代器在该集合上返回一个元素 e2,使得 ( e1==null ? e2==null : e1.equals(e2))。
句法:
public boolean equals(Object O)
参数:此函数接受一个强制参数 Object O ,它是 CopyOnWriteArraySet 要与之比较的对象。
返回值:如果比较对象等于此 Set,则该函数返回true 。
方案一:
Java
// Java Program to illustrate the
// equals() method in CopyOnWriteArraySet
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// create object of CopyOnWriteArraySet
CopyOnWriteArraySet ArrSet1
= new CopyOnWriteArraySet();
// Add elements
ArrSet1.add(32);
ArrSet1.add(67);
ArrSet1.add(98);
ArrSet1.add(100);
// print CopyOnWriteArraySet
System.out.println("CopyOnWriteArraySet 1: "
+ ArrSet1);
// create object of CopyOnWriteArraySet
CopyOnWriteArraySet ArrSet2
= new CopyOnWriteArraySet();
// Add elements
ArrSet2.add(32);
ArrSet2.add(67);
ArrSet2.add(98);
ArrSet2.add(100);
// print CopyOnWriteArraySet
System.out.println("CopyOnWriteArraySet 2: "
+ ArrSet2);
// compares both using equal() function
if (ArrSet1.equals(ArrSet2))
System.out.println("Both are Equal");
else
System.out.println("Both are not Equal");
}
}
Java
// Java Program to illustrate the
// equals() method in CopyOnWriteArraySet
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// create object of CopyOnWriteArraySet
CopyOnWriteArraySet ArrSet1
= new CopyOnWriteArraySet();
// Add elements
ArrSet1.add(32);
ArrSet1.add(67);
ArrSet1.add(98);
ArrSet1.add(100);
// print CopyOnWriteArraySet
System.out.println("CopyOnWriteArraySet 1: "
+ ArrSet1);
// create object of CopyOnWriteArraySet
CopyOnWriteArraySet ArrSet2
= new CopyOnWriteArraySet();
// Add elements
ArrSet2.add(3);
ArrSet2.add(6);
ArrSet2.add(9);
ArrSet2.add(10);
// print CopyOnWriteArraySet
System.out.println("CopyOnWriteArraySet 2: "
+ ArrSet2);
// compares both using equal() function
if (ArrSet1.equals(ArrSet2))
System.out.println("Both are Equal");
else
System.out.println("Both are not Equal");
}
}
输出:
CopyOnWriteArraySet 1: [32, 67, 98, 100]
CopyOnWriteArraySet 2: [32, 67, 98, 100]
Both are Equal
方案二:
Java
// Java Program to illustrate the
// equals() method in CopyOnWriteArraySet
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// create object of CopyOnWriteArraySet
CopyOnWriteArraySet ArrSet1
= new CopyOnWriteArraySet();
// Add elements
ArrSet1.add(32);
ArrSet1.add(67);
ArrSet1.add(98);
ArrSet1.add(100);
// print CopyOnWriteArraySet
System.out.println("CopyOnWriteArraySet 1: "
+ ArrSet1);
// create object of CopyOnWriteArraySet
CopyOnWriteArraySet ArrSet2
= new CopyOnWriteArraySet();
// Add elements
ArrSet2.add(3);
ArrSet2.add(6);
ArrSet2.add(9);
ArrSet2.add(10);
// print CopyOnWriteArraySet
System.out.println("CopyOnWriteArraySet 2: "
+ ArrSet2);
// compares both using equal() function
if (ArrSet1.equals(ArrSet2))
System.out.println("Both are Equal");
else
System.out.println("Both are not Equal");
}
}
输出:
CopyOnWriteArraySet 1: [32, 67, 98, 100]
CopyOnWriteArraySet 2: [3, 6, 9, 10]
Both are not Equal
参考: https: Java/util/concurrent/CopyOnWriteArraySet.html#equals-java.lang.Object-