public abstract class PeekIterator<T> extends Object implements Iterator<T>, Iterable<T>, Closeable
PeekIterator i=new SimplePeekIterator(1,2,3,4);
i.peek();
---> 1
i.peek();
---> 1
i.next();
---> 1
i.peek();
---> 2
The class is also suited to create an Interator by overriding. The only method that
needs top be overwritten is "internalNext()".
// An iterator over the numbers 0,1,2
PeekIterator it=new PeekIterator() {
int counter=0;
// Returns null if there are no more elements
protected Integer internalNext() throws Exception {
if(counter==3) return(null);
return(counter++);
}
};
for(Integer i : it) D.p(i);
--->
0
1
2
| Modifier and Type | Class and Description |
|---|---|
static class |
PeekIterator.ElementaryPeekIterator<T>
A Peek iterator with one single element
|
static class |
PeekIterator.SimplePeekIterator<T>
A PeekIterator that can iterate over another iterator or over a list of elements
|
| Modifier and Type | Field and Description |
|---|---|
boolean |
closed
TRUE if the iterator has been closed
|
protected static PeekIterator<Object> |
EMPTY
An empty PeekIterator
|
boolean |
fetchedNextValue
TRUE if variable next contains the next value
|
T |
next
Holds the next element (to be peeked)
|
| Constructor and Description |
|---|
PeekIterator() |
| Modifier and Type | Method and Description |
|---|---|
List<T> |
asList()
Returns an arraylist of this iterator (killing this iterator)
|
static <T> List<T> |
asList(Iterator<T> i)
Returns an arraylist of an iterator (killing the iterator)
|
Set<T> |
asSet()
Returns a hashset of this iterator (killing this iterator)
|
static <T> Set<T> |
asSet(Iterator<T> i)
Returns a hashset of an iterator (killing the iterator)
|
static <T> Set<T> |
asSet(Iterator<T> i,
Set<T> set)
Fills the elements of an iterator into a given set (killing the iterator)
|
void |
close()
Closes the underlying resource
|
static <K> PeekIterator<K> |
emptyIterator()
returns a constant empty iterator
|
boolean |
hasNext()
TRUE if there are more elements to get with getNext
|
protected abstract T |
internalNext()
Returns the next or NULL if no next element is available
|
protected T |
internalSilentNext()
Wraps the Exceptions of internalNext into RuntimeExceptions
|
Iterator<T> |
iterator()
returns this
|
static <S> List<S> |
list(Iterable<S> it)
Lists the elements in an iterable
|
static <S> List<S> |
list(Iterator<S> it)
Lists the elements in an iterator (and destroys it)
|
static void |
main(String[] args)
test routine
|
T |
next()
Returns the next element and advances.
|
T |
nextOrNull()
Returns the next element and advances.
|
static <S> int |
numElements(Iterable<S> it)
Counts the number of elements in an iterable
|
static <S> int |
numElements(Iterator<S> it)
Counts the number of elements in an iterator (and destroys it)
|
T |
peek()
returns the next element without advancing
|
void |
remove()
Removes the current element, if supported by the underlying iterator
|
String |
toString() |
static <S> StringBuilder |
toString(Iterable<S> it)
Lists the elements in an iterable
|
static <S> StringBuilder |
toString(Iterator<S> it)
Lists the elements in an iterator (and destroys it)
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitforEachRemainingforEach, spliteratorpublic T next
public boolean fetchedNextValue
public boolean closed
protected static PeekIterator<Object> EMPTY
public final boolean hasNext()
protected final T internalSilentNext()
protected abstract T internalNext() throws Exception
Exceptionpublic final T next()
public final T nextOrNull()
public void remove()
public final T peek()
public void close()
close in interface Closeableclose in interface AutoCloseablepublic static <T> List<T> asList(Iterator<T> i)
public static <T> Set<T> asSet(Iterator<T> i, Set<T> set)
public static <T> Set<T> asSet(Iterator<T> i)
public static <K> PeekIterator<K> emptyIterator()
public static <S> int numElements(Iterator<S> it)
public static <S> int numElements(Iterable<S> it)
public static <S> StringBuilder toString(Iterable<S> it)
public static <S> StringBuilder toString(Iterator<S> it)
public static <S> List<S> list(Iterator<S> it)
Copyright © 2018. All rights reserved.