欢迎访问宙启技术站
智能推送

Java中如何实现求两个数组的交集和并集?

发布时间:2023-11-11 21:43:58

在Java中,可以使用多种方法实现求两个数组的交集和并集。下面详细介绍三种常见的实现方法。

方法一:使用集合类库

Java中的集合类库提供了许多有用的方法,可以方便地进行数组操作。以下是使用集合类库实现求两个数组的交集和并集的代码:

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ArrayIntersectionUnion {
    public static void main(String[] args) {
        int[] array1 = {1, 2, 3, 4, 5};
        int[] array2 = {4, 5, 6, 7, 8};

        // 求交集
        Set<Integer> intersectionSet = new HashSet<>();
        for (int num : array1) {
            intersectionSet.add(num);
        }
        intersectionSet.retainAll(toSet(array2));
        System.out.println("交集:");
        for (int num : intersectionSet) {
            System.out.print(num + " ");
        }

        // 求并集
        Set<Integer> unionSet = new HashSet<>();
        unionSet.addAll(toSet(array1));
        unionSet.addAll(toSet(array2));
        System.out.println("
并集:");
        for (int num : unionSet) {
            System.out.print(num + " ");
        }
    }

    private static Set<Integer> toSet(int[] array) {
        Set<Integer> set = new HashSet<>();
        for (int num : array) {
            set.add(num);
        }
        return set;
    }
}

方法二:使用数组排序和双指针

另一种方法是对两个数组进行排序,然后使用双指针来查找交集和并集。以下是代码示例:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArrayIntersectionUnion {
    public static void main(String[] args) {
        int[] array1 = {1, 2, 3, 4, 5};
        int[] array2 = {4, 5, 6, 7, 8};

        // 求交集
        Arrays.sort(array1);
        Arrays.sort(array2);
        int i = 0, j = 0;
        List<Integer> intersectionList = new ArrayList<>();
        while (i < array1.length && j < array2.length) {
            if (array1[i] == array2[j]) {
                intersectionList.add(array1[i]);
                i++;
                j++;
            } else if (array1[i] < array2[j]) {
                i++;
            } else {
                j++;
            }
        }
        System.out.println("交集:" + intersectionList);

        // 求并集
        i = 0;
        j = 0;
        List<Integer> unionList = new ArrayList<>();
        while (i < array1.length && j < array2.length) {
            if (array1[i] == array2[j]) {
                unionList.add(array1[i]);
                i++;
                j++;
            } else if (array1[i] < array2[j]) {
                unionList.add(array1[i]);
                i++;
            } else {
                unionList.add(array2[j]);
                j++;
            }
        }
        while (i < array1.length) {
            unionList.add(array1[i]);
            i++;
        }
        while (j < array2.length) {
            unionList.add(array2[j]);
            j++;
        }
        System.out.println("并集:" + unionList);
    }
}

方法三:使用Java 8 Stream API

在Java 8中,引入了Stream API,可以更简洁地实现求两个数组的交集和并集。以下是使用Stream API实现的代码:

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

public class ArrayIntersectionUnion {
    public static void main(String[] args) {
        int[] array1 = {1, 2, 3, 4, 5};
        int[] array2 = {4, 5, 6, 7, 8};

        // 求交集
        Set<Integer> intersectionSet = Arrays.stream(array1)
                .boxed()
                .collect(Collectors.toSet());
        intersectionSet.retainAll(Arrays.stream(array2).boxed().collect(Collectors.toSet()));
        System.out.println("交集:" + intersectionSet);

        // 求并集
        Set<Integer> unionSet = new HashSet<>();
        unionSet.addAll(Arrays.stream(array1).boxed().collect(Collectors.toSet()));
        unionSet.addAll(Arrays.stream(array2).boxed().collect(Collectors.toSet()));
        System.out.println("并集:" + unionSet);
    }
}

以上是三种常见的实现方法,根据具体的需求可以选择合适的方法来求两个数组的交集和并集。