编程语言
首页 > 编程语言> > Java Collections-Collections.disjoint方法检查两个指定的集合是否不相交

Java Collections-Collections.disjoint方法检查两个指定的集合是否不相交

作者:互联网

Java Collections类的disjoint()方法用于检查两个指定的集合是否不相交。如果两个指定的集合没有相同的元素,则返回true。

Method:

public static boolean disjoint(Collection<?> c1,
                               Collection<?> c2)

Returns true if the two specified collections have no elements in common.

 

Examples

 

package com.logicbig.example.collections;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class DisjointExample {

    public static void main(String... args) {
        List<Integer> list = Arrays.asList(1, 2, 3, 4);
        List<Integer> list2 = Arrays.asList(10, 20, 30, 40, 50, 60);
        boolean b = Collections.disjoint(list, list2);
        System.out.println(b);
    }
}

Output

true

 

 

package com.logicbig.example.collections;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class DisjointExample2 {

    public static void main(String... args) {
        List<Integer> list = Arrays.asList(1, 2, 3, 4);
        List<Integer> list2 = Arrays.asList(10, 20, 30, 4, 50, 60);
        boolean b = Collections.disjoint(list, list2);
        System.out.println(b);
    }
}

Output

false

标签:Java,java,Arrays,List,util,Collections,disjoint,import
来源: https://blog.csdn.net/allway2/article/details/115053282