Java Iterator Size size(final Iterator iter)

Here you can find the source of size(final Iterator iter)

Description

Returns the number of elements in the iterator sequence.

License

Apache License

Parameter

Parameter Description
E the element type
iter the iterator

Return

the number of elements

Declaration

public static <E> int size(final Iterator<E> iter) 

Method Source Code

//package com.java2s;
/**/*from www.ja va  2s.co  m*/
 * 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 {
    /**
     * Returns the number of elements in the iterator sequence.
     * 
     * The iterator cannot be re-used after calling this function!!
     *
     * @param <E> the element type
     * @param iter the iterator
     * @return the number of elements
     */
    public static <E> int size(final Iterator<E> iter) {
        if (iter == null) {
            throw new NullPointerException();
        }
        int count = 0;
        while (iter.hasNext()) {
            iter.next();
            count++;
        }
        return count;
    }
}

Related

  1. count(final Iterator iterator)
  2. count(Iterator triples)
  3. count(Iterator iterator)
  4. count(Iterator it)
  5. counter(final Iterator iterator)
  6. size(Iterator source)
  7. size(Iterator iterator)
  8. size(Iterator iterator)