Java Utililty Methods Iterator

List of utility methods to do Iterator

Description

The list of methods to do Iterator are organized into topic(s).

Method

IteratorsingletonIterator(final Object item)
singleton Iterator
return new Iterator() {
    private boolean gotItem = false;
    public boolean hasNext() {
        return !this.gotItem;
    public Object next() {
        if (this.gotItem) {
            throw new NoSuchElementException();
...
IteratorsingletonIterator(final T item)
Returns a singleton iterator with a single item.
return new Iterator<T>() {
    private T _item = item;
    private boolean _hasItem = false;
    @Override
    public boolean hasNext() {
        return !_hasItem;
    @Override
...
IteratorsingletonIterator(final T nullableValue)
singleton Iterator
if (nullableValue == null) {
    return (Iterator<T>) EMPTY_ITERATOR;
return new Iterator<T>() {
    boolean done;
    @Override
    public boolean hasNext() {
        return !done;
...
IteratorsingletonIterator(final T t)
singleton Iterator
return Collections.singletonList(t).iterator();
intsize(Iterator iterator)
size
int count = 0;
while (iterator.hasNext()) {
    iterator.next();
    count++;
return count;
voidsplice(LinkedList list, Iterator iterator, LinkedList list2, V v)
splice
list2.remove(v);
assert (iterator.equals(list.iterator()));
list.addFirst(v);
Object[]toArray(final Iterator iterator)
to Array
final List<Object> out = new ArrayList<Object>();
while (iterator.hasNext()) {
    out.add(iterator.next());
return out.toArray();
voidtoCollection(final Iterator iterator, final Collection c)
to Collection
while (iterator.hasNext()) {
    c.add(iterator.next());
StringtoCommaSeparatedString(Iterator i)
to Comma Separated String
StringBuilder sb = new StringBuilder();
for (; i.hasNext();) {
    sb.append(i.next().toString());
    if (i.hasNext() == true) {
        sb.append(", ");
return sb.toString();
...
IteratortoIterator(Collection col)
Get an iterator from a collection, returning null if collection is null
if (col == null)
    return null;
else
    return col.iterator();