Java Iterator contains(final Iterator iter, final E item)

Here you can find the source of contains(final Iterator iter, final E item)

Description

Checks if an element item is contained in the iterator sequence.

License

Apache License

Parameter

Parameter Description
E the element type
iter the iterator
item the item

Return

true, if the element is in the iterator sequence

Declaration

public static <E> boolean contains(final Iterator<? extends E> iter, final E item) 

Method Source Code

//package com.java2s;
/**// ww w  .j a va  2 s .com
 * Licensed to the TomTom International B.V. under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  TomTom International B.V.
 * licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.util.Iterator;

public class Main {
    /**
     * Checks if an element item is contained in the iterator sequence.
     * 
     * The iterator cannot be re-used after calling this function!!
     *
     * @param <E> the element type
     * @param iter the iterator
     * @param item the item
     * @return true, if the element is in the iterator sequence
     */
    public static <E> boolean contains(final Iterator<? extends E> iter, final E item) {
        if (iter == null || item == null) {
            throw new NullPointerException();
        }
        while (iter.hasNext()) {
            if (item.equals(iter.next())) {
                return true;
            }
        }
        return false;
    }
}

Related

  1. closeIterator(Iterator iterator)
  2. compareRanges(final Iterator i, final Iterator j)
  3. concatinateElements(Iterator it)
  4. concatIterators(final Iterator... iterators)
  5. concatLines(Iterator lineIterator)
  6. contains(Iterator it, T o)
  7. contains(Iterator iterator, Object element)
  8. contains(Iterator iterator, String value)
  9. convertToArray(Iterator iter)