Here you can find the source of addAll(Collection
Parameter | Description |
---|---|
coll1 | a parameter |
coll2 | a parameter |
public static <T, C extends Collection<T>> C addAll(Collection<T> coll1, Collection<T> coll2)
//package com.java2s; import java.util.ArrayList; import java.util.Collection; public class Main { /**/*from w w w . j av a2s.co m*/ * A potentially more efficient addAll() for unordered Collections. * @param coll1 * @param coll2 * @return the longer of the two collections after adding the shorter to the longer. */ public static <T, C extends Collection<T>> C addAll(Collection<T> coll1, Collection<T> coll2) { if (coll1 == null) return (C) coll2; if (coll2 == null) return (C) coll1; Collection<T> cSmaller, cBigger; if (coll1.size() < coll2.size()) { cSmaller = coll1; cBigger = coll2; } else { cSmaller = coll2; cBigger = coll1; } try { cBigger.addAll(cSmaller); return (C) cBigger; } catch (UnsupportedOperationException e) { } try { cSmaller.addAll(cBigger); return (C) cSmaller; } catch (UnsupportedOperationException e) { } ArrayList<T> newList = new ArrayList<T>(cBigger); newList.addAll(cSmaller); return (C) newList; } }