Java judges whether two sets have intersection and how to get the intersection

1. Java judges whether two sets have intersection

1、Collections.disjoint

The fully qualified name is java.util.Collections, and it will return true when the two collections do not intersect, otherwise it will return false.

for example:

List<String> firstList = Arrays.asList("teacher", "worker", "student");
List<String> secondList = Arrays.asList("user", "admin");

if (Collections.disjoint(firstList, secondList)) {
    
    
    System.out.println("Collections.disjoint方法:firstList、secondList没有交集");
}

result:
insert image description here

2、CollectionUtils.containsAny

The fully qualified name is: org.apache.commons.collections.CollectionUtils. If two collections intersect, it will return true, otherwise it will return false, which is the opposite of Collections.disjoint.

For example:

List<String> firstList = Arrays.asList("teacher", "worker", "student");
List<String> secondList = Arrays.asList("user", "admin");

if (CollectionUtils.containsAny(firstList, secondList)) {
    
    
    System.out.println("CollectionUtils.containsAny方法:firstList、secondList有交集");
} else if (!CollectionUtils.containsAny(firstList, secondList)) {
    
    
    System.out.println("CollectionUtils.containsAny方法:firstList、secondList没有交集");
}

result:
insert image description here

3、CollectionUtil.containsAny

The fully qualified name is: cn.hutool.core.collection.CollectionUtil. If two collections intersect, it will return true, otherwise it will return false, which is the opposite of Collections.disjoint.

for example:

 List<String> firstList = Arrays.asList("teacher", "worker", "student");
 List<String> secondList = Arrays.asList("user", "admin");

 if (CollectionUtil.containsAny(firstList, secondList)) {
    
    
     System.out.println("CollectionUtil.containsAny方法:firstList、secondList有交集");
 } else if (!CollectionUtil.containsAny(firstList, secondList)) {
    
    
     System.out.println("CollectionUtil.containsAny方法:firstList、secondList没有交集");
 }

result:
insert image description here

4. Use the new features of Java8

There are two ways in Java8 to get whether two collections intersect, as follows:

List<String> firstList = Arrays.asList("teacher", "worker", "student");
List<String> secondList = Arrays.asList("user", "admin");

//方式一
//List<String> resultList = firstList.stream().filter((firstItem) -> secondList.contains(firstItem)).collect(Collectors.toList());

//方式二
List<String> resultList = firstList.stream().filter(secondList::contains).collect(Collectors.toList());
if (resultList != null && resultList.size() > 0) {
    
    
    System.out.println("firstList、secondList有交集");
} else {
    
    
    System.out.println("firstList、secondList没有交集");
}

result:
insert image description here

2. Obtain the intersection of two sets

1. Use for loop

for example:

public static void main(String[] args) {
    
    
    List<String> firstList = Arrays.asList("teacher", "worker", "student", "driver");
    List<String> secondList = Arrays.asList("user", "admin", "student", "driver");

    List<String> resultList = new ArrayList<>();
    for (String item : firstList) {
    
    
        if (secondList.contains(item)) {
    
    
            resultList.add(item);
        }
    }
    System.out.println(resultList);
}

result:
insert image description here

If there are duplicate elements in the two collections, the result obtained in this way is not deduplicated, if it is:
insert image description here

turn out:
insert image description here

If you want to add weight, you can add
insert image description here

The final code:

public static void main(String[] args) {
    
    
    List<String> firstList = Arrays.asList("teacher", "worker", "student", "driver", "driver");
    List<String> secondList = Arrays.asList("user", "admin", "student", "driver", "driver");

    List<String> resultList = new ArrayList<>();
    for (String item : firstList) {
    
    
        if (secondList.contains(item) && !resultList.contains(item)) {
    
    
            resultList.add(item);
        }
    }
    System.out.println(resultList);
}

The result obtained in this way is deduplication.
insert image description here

You can also use HashSet to deduplicate
insert image description here

result:
insert image description here

2. Use Java8's forEach

for example:

public static void main(String[] args) {
    
    
    List<String> firstList = Arrays.asList("teacher", "worker", "student", "driver");
    List<String> secondList = Arrays.asList("user", "admin", "student", "driver");

    List<String> resultList = new ArrayList<>();
    if (firstList == null) {
    
    
        throw new RuntimeException("firstList为空!");
    }
    
    firstList.forEach((firstItem) -> {
    
    
        if (secondList.contains(firstItem)) {
    
    
            resultList.add(firstItem);
        }
    });
    System.out.println(resultList);
}

result:
insert image description here

If two sets have the same elements
insert image description here

Like "2.1", it cannot be deduplicated
insert image description here

The method is also the same as above
insert image description here

result:
insert image description here

Or use HashSet, which is the same as "2.1".

3. Use the new features of Java8

Like "1.4", there are two ways to get the intersection of two sets.

code show as below:

public static void main(String[] args) {
    
    
    List<String> firstList = Arrays.asList("teacher", "worker", "student");
    List<String> secondList = Arrays.asList("user", "admin", "student");

    //方式一
    List<String> resultList = firstList.stream().filter((firstItem) -> secondList.contains(firstItem)).collect(Collectors.toList());

    //方式二
    //List<String> resultList = firstList.stream().filter(secondList::contains).collect(Collectors.toList());
    System.out.println(resultList);
}

result:
insert image description here

Like "2.1" and "2.2", it is also impossible to remove repeated elements
insert image description here

result:
insert image description here

Adding distinct can deduplicate
insert image description here
the result:
insert image description here

Guess you like

Origin blog.csdn.net/studio_1/article/details/129873235