Java Array Intersect intersect(int[]... arrays)

Here you can find the source of intersect(int[]... arrays)

Description

Returns the intersection vector of a series of input arrays.

License

Open Source License

Parameter

Parameter Description
arrays Vector of input arrays

Return

Intersection vector of input arrays

Declaration

public static int[] intersect(int[]... arrays) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2016 Pablo Pavon-Marino.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * Contributors://  w w w. java 2  s  . co  m
 *     Pablo Pavon-Marino - Jose-Luis Izquierdo-Zaragoza, up to version 0.3.1
 *     Pablo Pavon-Marino - from version 0.4.0 onwards
 ******************************************************************************/

import java.util.*;

public class Main {
    /**
     * Returns the intersection vector of a series of input arrays. There is no order guarantee.
     *
     * @param arrays Vector of input arrays
     * @return Intersection vector of input arrays
     */
    public static int[] intersect(int[]... arrays) {
        if (arrays.length == 0)
            return new int[0];

        Set<Integer> intersectionSet = new LinkedHashSet<Integer>();
        intersectionSet.addAll(toList(arrays[0]));

        for (int i = 1; i < arrays.length; i++)
            intersectionSet.retainAll(toList(arrays[i]));

        return toArray(intersectionSet);
    }

    /**
     * Converts from a {@code int} array to a list.
     *
     * @param array Input array
     * @return A list of {@code Integer} objects
     *  
     */
    public static List<Integer> toList(int[] array) {
        List<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < array.length; i++)
            list.add(array[i]);

        return list;
    }

    /**
     * Converts an array of {@code String} with the values, into a {@code int} array. Uses {@code Integer.parseInt} to parse the number, an can raise the exceptions 
     * that this method raises
     *
     * @param list Input list
     * @return output array
     */
    public static int[] toArray(String[] list) {
        int[] res = new int[list.length];
        int counter = 0;
        for (String s : list)
            res[counter++] = Integer.parseInt(s);
        return res;
    }

    /**
     * Converts a collection ({@code List}, {@code Set}...) of {@code Integer} objects to an {@code int} array.
     *
     * @param list Input list
     * @return {@code int} array
     *  
     */
    public static int[] toArray(Collection<Integer> list) {
        return asPrimitiveArray(list.toArray(new Integer[list.size()]));
    }

    /**
     * Converts from an {@code Integer} array to an {@code int} array.
     *
     * @param array {@code Integer} array
     * @return Equivalent {@code int} array
     */
    public static int[] asPrimitiveArray(Integer[] array) {
        int[] primitiveArray = new int[array.length];

        for (int i = 0; i < array.length; i++)
            primitiveArray[i] = array[i];

        return primitiveArray;
    }
}

Related

  1. getArrayIntersection(int a[], int b[])
  2. getNonIntersection(int[] interval, int[] intervalToRemove)
  3. hasIntersection(String a1[], String a2[], int mode)
  4. intersect(boolean[] mask, int[] examples, boolean[] intersection)
  5. intersect(int[] sorted1, int[] sorted2)
  6. intersect(String[] arr1, String[] arr2)
  7. intersect(String[] list1, String[] list2)
  8. intersect(T[] a, T[] b)
  9. intersect(T[] a, T[] b)