Java examples for Collection Framework:Iterator
Limits an iterator to a given count.
//package com.java2s; import java.util.Iterator; public class Main { /**/*w w w . j av a 2 s . c o m*/ * Limits an iterator to a given count. */ public static <T> Iterator<T> limitIterator(final Iterator<T> it, final long limit) { return new Iterator<T>() { long left = limit; @Override public void remove() { } @Override public T next() { left--; return it.next(); } @Override public boolean hasNext() { return left > 0 && it.hasNext(); } }; } }