java两个数组的值比较求交集、差集、并集

/**
     * 两个数组比较,求交集
     * @author ZHENGKANG
     * @date 2018年12月29日
     * @param strOne
     * @param strTwo
     * @return Object[]
     */
    public Object[] intersection(Object[] strOne, Object[] strTwo) {
        if (ArrayUtils.isEmpty(strOne) || ArrayUtils.isEmpty(strTwo)) {
            return null;
        }
        HashSet set = new HashSet<>(Arrays.asList(strOne));
        set.retainAll(Arrays.asList(strTwo));
        return set.toArray();
    }
    
    /**
     * 两个数组比较,求差集
     * @author ZHENGKANG
     * @date 2018年12月29日
     * @param strOne
     * @param strTwo
     * @return Object[]
     */
    public Object[] differenceSet(Object[] strOne, Object[] strTwo) {
        if (ArrayUtils.isEmpty(strOne) || ArrayUtils.isEmpty(strTwo)) {
            return null;
        }
        HashSet set = new HashSet<>(Arrays.asList(strOne));
        set.removeAll(new HashSet<>(Arrays.asList(strTwo)));
        return set.toArray();
    }
    
    /**
     * 两个数组比较,求并集(去重)
     * @author ZHENGKANG
     * @date 2018年12月29日
     * @param strOne
     * @param strTwo
     * @return Object[]
     */
    public Object[] union(Object[] strOne, Object[] strTwo) {
        if (ArrayUtils.isEmpty(strOne) && ArrayUtils.isEmpty(strTwo)) {
            return null;
        } else if (ArrayUtils.isNotEmpty(strOne) && ArrayUtils.isEmpty(strTwo)) {
            return strOne;
        } else if (ArrayUtils.isEmpty(strOne) && ArrayUtils.isNotEmpty(strTwo)) {
            return strTwo;
        }
        HashSet set = new HashSet<>(Arrays.asList(strOne));
        set.addAll(new HashSet<>(Arrays.asList(strTwo)));
        return set.toArray();
    }

你可能感兴趣的:(Java)