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
上一篇:915. Partition Array into Disjoint Intervals


下一篇:C语言实现并查集(Disjoint set或者Union-find set)(附完整源码)