Java Collections disjoint()方法与示例

集合类disjoint()方法 (Collections Class disjoint() method)

  • disjoint() method is available in java.util package.

    disjoint()方法在java.util包中可用。

  • disjoint() method is used to check whether the given Collection objects may contain any common elements or not.

    disjoint()方法用于检查给定的Collection对象是否可以包含任何公共元素。

  • disjoint() method is a static method, so it is accessible with the class name and if we try to access the method with the class object then we will not get an error.

    disjoint()方法是一个静态方法,因此可以使用类名进行访问,如果尝试使用类对象访问该方法,则不会收到错误。

  • disjoint() method may throw an exception at the time of checking no common element exists.

    在检查不存在公共元素时, disjoint()方法可能会引发异常。

    NullPointerException: This exception may throw when the given parameter is null exists.

    NullPointerException :当给定参数为null时,可能引发此异常。

Syntax:

句法:

    public static boolean disjoint(Collection cl1, Collection cl2);

Parameter(s):

参数:

  • Collection cl1, Collection cl2 – represent the different Collection objects.

    集合cl1,集合cl2 –表示不同的集合对象。

Return value:

返回值:

The return type of this method is boolean, it returns true when no common element exists in Collection object otherwise it returns false.

此方法的返回类型为boolean ,当Collection对象中不存在公共元素时返回true,否则返回false。

Example:

例:

// Java program is to demonstrate the example
// of boolean disjoint() of Collections

import java.util.*;
public class Disjoint {
    public static void main(String args[]) {
        // Instantiate two LinkedList object
        List < Integer > l1 = new LinkedList < Integer > ();
        List < Integer > l2 = new LinkedList < Integer > ();

        // By using add() method is to add
        // few elements in l1
        l1.add(10);
        l1.add(20);
        l1.add(30);
        l1.add(40);

        // By using add() method is to add
        // few elements in l2
        l2.add(60);
        l2.add(70);
        l2.add(80);
        l2.add(90);

        // Display LinkedList
        System.out.println("l1: " + l1);
        System.out.println("l2: " + l2);

        // By using disjoint() method returns 
        // true when no common elements exists
        // in both the collections
        boolean status = Collections.disjoint(l1, l2);

        System.out.println();

        // Display status
        System.out.println("Collections.disjoint(l1,l2): " + status);
    }
}

Output

输出量

l1: [10, 20, 30, 40]
l2: [60, 70, 80, 90]

Collections.disjoint(l1,l2): true


翻译自: https://www.includehelp.com/java/collections-disjoint-method-with-example.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值