Or intersection of two sets of arrays, and the two sets, and different elements

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/shy415502155/article/details/89144488

The two arrays or two collections

Integer [] = {1,2,3,5,6} arr1;
Integer [] = {2,4,5,7} arr2;

Find the intersection between the two

List<Integer> list1 = Arrays.asList(arr1);
List<Integer> list2 = Arrays.asList(arr2);
// 创建集合 求交集
Collection<Integer> c1 = new ArrayList<Integer>(list1);
Collection<Integer> c2 = new ArrayList<Integer>(list2);
c1.retainAll(c2);
System.out.println("arr1与arr2交集结果:" + c1);

And set between the two requirements

Set result = new HashSet();
result.addAll(list1);
result.addAll(list2);
System.out.println("并集结果:" + result);

Seeking elements present arr1, arr2 does not exist, empathy is also available arr2 elements exist, arr1 does not exist

List<Integer> l1 = new ArrayList<Integer>();
for(int i = 0; i < len1; i++) {
    Integer num = list1.get(i);
    if (!list2.contains(num)) {
        l1.add(num);
    }
}
System.out.println("arr1集合中存在的元素但是arr2中不存在 :" + l1);

 

Guess you like

Origin blog.csdn.net/shy415502155/article/details/89144488