Here you can find the source of intersect(Collection... collections)
Parameter | Description |
---|---|
A | Key type |
collections | Series of input collections |
public static <A> Set<A> intersect(Collection<A>... collections)
//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:/*from ww w .j av a 2 s . c o 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 set of a series of input collections. There is no order guarantee. * * @param <A> Key type * @param collections Series of input collections * @return Intersection set of input collections */ public static <A> Set<A> intersect(Collection<A>... collections) { if (collections.length == 0) return new LinkedHashSet<A>(); Set<A> intersectionSet = new LinkedHashSet<A>(); intersectionSet.addAll(collections[0]); for (int i = 1; i < collections.length; i++) { intersectionSet.retainAll(collections[i]); } return intersectionSet; } }