TLists { @SafeVarargs public static void retainIntersection(List<?>... lists) { Preconditions.checkArgument(lists.length > 0); List<?> intersection = lists[0]; for (int i = 1; i < lists.length; ++i) { intersection.retainAll(lists[i]); } for (int i = 1; i < lists.length; ++i) { lists[i].retainAll(intersection); } } private  TLists(); @SafeVarargs static void retainIntersection(List<?>... lists); @SuppressWarnings("unchecked") static T[] toArrayCasted(List<T> list, Class<T> clazz);  }
@SuppressWarnings("unchecked") @Test public void testRetainIntersection() { List<Integer> common = Lists.newArrayList(); List<Integer>[] lists = new List[Random.getScaleCount()]; int max = Random.getScaleCount(); for (int i = 0; i < max; i++) { common.add(i); } for (int i = 0; i < lists.length; i++) { lists[i] = Lists.<Integer> newArrayList(); lists[i].addAll(common); for (int j = 0; j < Random.getScaleCount(); j++) { lists[i].add(Random.getScaleCount() + max); } lists[i] = Lists.newArrayList(lists[i]); } TLists.retainIntersection(lists); for (List<Integer> list : lists) { Assert.assertEquals(common, list); } }