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

voidcloseIterator(Iterator iterator)
Closes an iterator if it has interface ClosableIterator.
while (iterator.hasNext()) {
    iterator.next();
intcompareRanges(final Iterator i, final Iterator j)
compare Ranges
while (i.hasNext() && j.hasNext()) {
    final E a = i.next();
    final E b = j.next();
    final int ret = a.compareTo(b);
    if (ret != 0) {
        return ret;
if (i.hasNext()) {
    return 1;
if (j.hasNext()) {
    return -1;
return 0;
StringconcatinateElements(Iterator it)
concatinate Elements
StringBuffer sb = new StringBuffer();
boolean first = true;
while (it.hasNext()) {
    if (!first) {
        sb.append(", ");
    Object next = it.next();
    String elm = next == null ? "<null>" : next.toString();
...
IteratorconcatIterators(final Iterator... iterators)
Concatenate a number of iterators together, to form one big iterator.
return new Iterator<E>() {
    Iterator<E> lastIter = null;
    List<Iterator<E>> iters = new LinkedList<>(Arrays.asList(iterators));
    @Override
    public boolean hasNext() {
        return !iters.isEmpty() && iters.get(0).hasNext();
    @Override
...
StringconcatLines(Iterator lineIterator)
Concatenates several lines into one string.
StringBuffer sb = new StringBuffer();
while (lineIterator.hasNext()) {
    String line = (String) lineIterator.next();
    if (line != null) {
        if (sb.length() != 0)
            sb.append('\n');
        sb.append(line);
return sb.length() != 0 ? sb.toString() : null;
booleancontains(final Iterator iter, final E item)
Checks if an element item is contained in the iterator sequence.
if (iter == null || item == null) {
    throw new NullPointerException();
while (iter.hasNext()) {
    if (item.equals(iter.next())) {
        return true;
return false;
booleancontains(Iterator it, T o)
contains
if (it == null) {
    throw new IllegalArgumentException("null it");
while (it.hasNext()) {
    if (o.equals(it.next())) {
        return true;
return false;
booleancontains(Iterator iterator, Object element)
Check whether the given Iterator contains the given element.
if (iterator != null) {
    while (iterator.hasNext()) {
        Object candidate = iterator.next();
        if (Objects.equals(candidate, element))
            return true;
return false;
...
booleancontains(Iterator iterator, String value)
Determines if a String value is included in a Iterated list.
if (iterator == null || value == null) {
    return false;
while (iterator.hasNext()) {
    if (value.equals(iterator.next())) {
        return true;
return false;
double[]convertToArray(Iterator iter)
convert To Array
return listToArray(convertToList(iter));