Java中的ConcurrentSkipListSet equals()方法

java的equals()方法。util。同时发生的ConcurrentSkipListSet是Java中的一个内置函数,它将指定的对象与该集合进行比较,以确定是否相等。如果指定的对象也是集合,则返回True。如果这两组满足以下所有条件,则称为相等:

null
  • 这两套尺寸相同。
  • 指定集合的每个成员都包含在此集合中。

此定义确保equals方法在set接口的不同实现中正常工作。

语法:

ConcurrentSkipListSet.equals(Object o)

参数: 该函数返回一个参数 o i、 e.要与该集合进行相等比较的对象

返回值: 该函数返回布尔值。如果指定的对象等于此集合,则返回true,否则返回false。

下面的程序演示了ConcurrentSkipListSet。equals()方法:

项目1: 在本例中,两个集合相等。

// Java Program Demonstrate equals()
// method of ConcurrentSkipListSet
import java.util.NavigableSet;
import java.util.concurrent.ConcurrentSkipListSet;
class ConcurrentSkipListSetEqualsExample1 {
public static void main(String[] args)
{
// Initializing the set
ConcurrentSkipListSet<Integer>
set = new ConcurrentSkipListSet<Integer>();
// Adding elements to this set
set.add( 10 );
set.add( 35 );
set.add( 20 );
set.add( 25 );
// Creating a descending set object
NavigableSet<Integer> des_set = set.descendingSet();
// Checking if the set and des
if (set.equals(des_set))
System.out.println( "Both the sets are equal" );
else
System.out.println( "Both the sets are not equal" );
// Printing the elements of the set
System.out.println( "Contents of the set: " + set);
// Printing the elements of the descending set
System.out.println( "Contents of the descending set: " +
des_set);
}
}


输出:

Both the sets are equal
Contents of the set: [10, 20, 25, 35]
Contents of the descending set: [35, 25, 20, 10]

项目2: 在这个例子中,两个集合并不相等

// Java Program Demonstrate equals()
// method of ConcurrentSkipListSet
import java.util.NavigableSet;
import java.util.concurrent.ConcurrentSkipListSet;
class ConcurrentSkipListSetEqualsExample2 {
public static void main(String[] args)
{
// Initializing the set
ConcurrentSkipListSet<Integer>
set1 = new ConcurrentSkipListSet<Integer>();
ConcurrentSkipListSet<Integer>
set2 = new ConcurrentSkipListSet<Integer>();
// Adding elements to first set
set1.add( 10 );
set1.add( 35 );
set1.add( 20 );
set1.add( 25 );
// Adding elements to second set
set2.add( 35 );
set2.add( 20 );
set2.add( 25 );
// Checking if the set and des
if (set1.equals(set2))
System.out.println( "Both the sets are equal" );
else
System.out.println( "Both the sets are not equal" );
// Printing the elements of the set
System.out.println( "Contents of the first set: " +
set1);
// Printing the elements of the descending set
System.out.println( "Contents of the second set: " +
set2);
}
}


输出:

Both the sets are not equal
Contents of the first set: [10, 20, 25, 35]
Contents of the second set: [20, 25, 35]

参考: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListSet.html#equals-爪哇。lang.反对-

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享