Here you can find the source of singletonIterator(final T item)
Parameter | Description |
---|---|
T | the item kind. |
item | the item. |
public static <T> Iterator<T> singletonIterator(final T item)
//package com.java2s; //License from project: Apache License import java.util.Iterator; import java.util.NoSuchElementException; public class Main { /**/*from w w w . jav a 2 s . c o m*/ * Returns a singleton iterator with a single item. * * @param <T> the item kind. * @param item the item. * @return a singleton iterator with a single item. */ public static <T> Iterator<T> singletonIterator(final T item) { return new Iterator<T>() { private T _item = item; private boolean _hasItem = false; @Override public boolean hasNext() { return !_hasItem; } @Override public T next() { if (_hasItem) { throw new NoSuchElementException(); } _hasItem = true; return _item; } @Override public void remove() { if (!_hasItem) { _hasItem = true; } else { throw new NoSuchElementException(); } } }; } }