/** @see Iterate#take(Iterable, int) */ public static <T, R extends Collection<T>> R take( Iterable<T> iterable, int count, R targetCollection) { if (count < 0) { throw new IllegalArgumentException("Count must be greater than zero, but was: " + count); } Iterator<T> iterator = iterable.iterator(); while (iterator.hasNext() && count-- > 0) { targetCollection.add(iterator.next()); } return targetCollection; }
/** @see Iterate#drop(Iterable, int) */ public static <T, R extends Collection<T>> R drop( Iterable<T> iterable, int count, R targetCollection) { if (count < 0) { throw new IllegalArgumentException("Count must be greater than zero, but was: " + count); } Iterator<T> iterator = iterable.iterator(); for (int i = 0; iterator.hasNext(); i++) { T element = iterator.next(); if (i >= count) { targetCollection.add(element); } } return targetCollection; }