Java Reference
In-Depth Information
/**
* Creates a new instance of an {@link java.util.Iterator}.
* The iterator should be a fresh iterator each time this
* method is called, but it should always iterate over the
* same source.
*
* @return A new {@code Iterator}; never {@code null}.
*/
Iterator<E> create();
}
public LambdaCollection(int size, IteratorFactory<E> factory) {
this.size = size;
this.factory = factory;
}
/**
* Returns an iterator over the elements contained in this collection.
*
* @return an iterator over the elements contained in this collection
*/
@Override
public Iterator<E> iterator() {
return factory.create();
}
@Override
public int size() {
return size;
}
}
public static Path generateFile() throws IOException {
File file = File.createTempFile("example", "tmp");
file.deleteOnExit();
Path path = file.toPath();
Files.write(path, "this\nis\nour\nfile".getBytes("UTF-8"));
return path;
}
public static void main(String[] args) throws Exception {
Path file = generateFile();
Collection<String> collection = new LambdaCollection<>(
(int) Files.lines(file).count(),
() -> {
Search WWH ::




Custom Search