Java Array Intersect intersection(final long[] array1, final long[] array2)

Here you can find the source of intersection(final long[] array1, final long[] array2)

Description

intersection

License

Open Source License

Declaration

public static long[] intersection(final long[] array1, final long[] array2) 

Method Source Code


//package com.java2s;
/*/*from ww  w .j  a  v  a  2  s.c om*/
 *             Twidere - Twitter client for Android
 * 
 *  Copyright (C) 2012-2014 Mariotaku Lee <mariotaku.lee@gmail.com>
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

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

public class Main {
    public static long[] intersection(final long[] array1, final long[] array2) {
        if (array1 == null || array2 == null)
            return new long[0];
        final List<Long> list1 = new ArrayList<Long>();
        for (final long item : array1) {
            list1.add(item);
        }
        final List<Long> list2 = new ArrayList<Long>();
        for (final long item : array2) {
            list2.add(item);
        }
        list1.retainAll(list2);
        return fromList(list1);
    }

    public static long[] fromList(final List<Long> list) {
        if (list == null)
            return null;
        final int count = list.size();
        final long[] array = new long[count];
        for (int i = 0; i < count; i++) {
            array[i] = list.get(i);
        }
        return array;
    }
}

Related

  1. intersect(String[] list1, String[] list2)
  2. intersect(T[] a, T[] b)
  3. intersect(T[] a, T[] b)
  4. intersectArrays(int[] a, int[] b)
  5. intersectArrays(int[] targetArray, int[] selfArray)
  6. intersection(int[] array1, int[] array2)
  7. intersection(String[] ary1, String[] ary2)
  8. intersection(String[] ss1, String[] ss2)
  9. intersectRanges(int[] range1, int[] range2)