@Test public void testArraysJoinSimple4() { String[] a1 = {}; String[] a2 = { "b", "y", "z" }; String[] a3 = { "b", "d", "e", "f" }; String[] result = ArrayUtils.intersect(a1, a2, a3); String[] expected = {}; assertTrue(Arrays.equals(expected, result)); }
@SuppressWarnings("unchecked") public static <T> T[] intersect(final T[]... arrays) { final Class<?> type = arrays.getClass().getComponentType().getComponentType(); if (arrays.length == 0) { return (T[]) Array.newInstance(type, 0); } final List<T> commonItems = new ArrayList<>(); final T[] firstArray = arrays[0]; commonItems.addAll(Arrays.asList(firstArray)); for (int i = 1; i < arrays.length; i++) { final T[] array = arrays[i]; final List<T> arrayAsList = Arrays.asList(array); final Set<T> itemsToRemove = new HashSet<>(); for (T item : commonItems) { if (!arrayAsList.contains(item)) { itemsToRemove.add(item); } } commonItems.removeAll(itemsToRemove); } T[] result = (T[]) Array.newInstance(type, commonItems.size()); result = commonItems.toArray(result); return result; }
ArrayUtils { @SuppressWarnings("unchecked") public static <T> T[] intersect(final T[]... arrays) { final Class<?> type = arrays.getClass().getComponentType().getComponentType(); if (arrays.length == 0) { return (T[]) Array.newInstance(type, 0); } final List<T> commonItems = new ArrayList<>(); final T[] firstArray = arrays[0]; commonItems.addAll(Arrays.asList(firstArray)); for (int i = 1; i < arrays.length; i++) { final T[] array = arrays[i]; final List<T> arrayAsList = Arrays.asList(array); final Set<T> itemsToRemove = new HashSet<>(); for (T item : commonItems) { if (!arrayAsList.contains(item)) { itemsToRemove.add(item); } } commonItems.removeAll(itemsToRemove); } T[] result = (T[]) Array.newInstance(type, commonItems.size()); result = commonItems.toArray(result); return result; } }
ArrayUtils { @SuppressWarnings("unchecked") public static <T> T[] intersect(final T[]... arrays) { final Class<?> type = arrays.getClass().getComponentType().getComponentType(); if (arrays.length == 0) { return (T[]) Array.newInstance(type, 0); } final List<T> commonItems = new ArrayList<>(); final T[] firstArray = arrays[0]; commonItems.addAll(Arrays.asList(firstArray)); for (int i = 1; i < arrays.length; i++) { final T[] array = arrays[i]; final List<T> arrayAsList = Arrays.asList(array); final Set<T> itemsToRemove = new HashSet<>(); for (T item : commonItems) { if (!arrayAsList.contains(item)) { itemsToRemove.add(item); } } commonItems.removeAll(itemsToRemove); } T[] result = (T[]) Array.newInstance(type, commonItems.size()); result = commonItems.toArray(result); return result; } private  ArrayUtils(); }
ArrayUtils { @SuppressWarnings("unchecked") public static <T> T[] intersect(final T[]... arrays) { final Class<?> type = arrays.getClass().getComponentType().getComponentType(); if (arrays.length == 0) { return (T[]) Array.newInstance(type, 0); } final List<T> commonItems = new ArrayList<>(); final T[] firstArray = arrays[0]; commonItems.addAll(Arrays.asList(firstArray)); for (int i = 1; i < arrays.length; i++) { final T[] array = arrays[i]; final List<T> arrayAsList = Arrays.asList(array); final Set<T> itemsToRemove = new HashSet<>(); for (T item : commonItems) { if (!arrayAsList.contains(item)) { itemsToRemove.add(item); } } commonItems.removeAll(itemsToRemove); } T[] result = (T[]) Array.newInstance(type, commonItems.size()); result = commonItems.toArray(result); return result; } private  ArrayUtils(); @SuppressWarnings("unchecked") static T[] arrayMerge(T[]... arrays); @SuppressWarnings("unchecked") static T[] intersect(final T[]... arrays); }
ArrayUtils { @SuppressWarnings("unchecked") public static <T> T[] intersect(final T[]... arrays) { final Class<?> type = arrays.getClass().getComponentType().getComponentType(); if (arrays.length == 0) { return (T[]) Array.newInstance(type, 0); } final List<T> commonItems = new ArrayList<>(); final T[] firstArray = arrays[0]; commonItems.addAll(Arrays.asList(firstArray)); for (int i = 1; i < arrays.length; i++) { final T[] array = arrays[i]; final List<T> arrayAsList = Arrays.asList(array); final Set<T> itemsToRemove = new HashSet<>(); for (T item : commonItems) { if (!arrayAsList.contains(item)) { itemsToRemove.add(item); } } commonItems.removeAll(itemsToRemove); } T[] result = (T[]) Array.newInstance(type, commonItems.size()); result = commonItems.toArray(result); return result; } private  ArrayUtils(); @SuppressWarnings("unchecked") static T[] arrayMerge(T[]... arrays); @SuppressWarnings("unchecked") static T[] intersect(final T[]... arrays);  }