Java ListIterator Usage valueOf(E... elements)

Here you can find the source of valueOf(E... elements)

Description

Returns an Iterator instance for the given elements

License

Apache License

Parameter

Parameter Description
elements a parameter

Return

new instance of an

Declaration

public static <E> Iterator<E> valueOf(E... elements) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2012 Danny Kunz//from   ww w  .  j a  va  2  s.  com
 * 
 * Licensed 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.ArrayList;
import java.util.Arrays;

import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class Main {
    /**
     * Returns the {@link Iterator} instances of the given {@link Iterable}s. If an {@link Iterator} instance is null it will not be
     * added to the returned array. This circumstance can lead to different array sizes.
     * 
     * @param iterables
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <E> Iterator<E>[] valueOf(Iterable<E>... iterables) {
        //    
        final List<Iterator<E>> retlist = new ArrayList<Iterator<E>>();

        //
        for (Iterable<E> iterable : iterables) {
            //
            final Iterator<E> iterator = iterable.iterator();
            if (iterator != null) {
                retlist.add(iterator);
            }
        }

        //
        return retlist.toArray(new Iterator[retlist.size()]);
    }

    /**
     * Returns the {@link ListIterator} instances of the given {@link List}s. If an {@link ListIterator} instance is null it will
     * not be added to the returned array. This circumstance can lead to different array sizes.
     * 
     * @param lists
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <E> ListIterator<E>[] valueOf(List<E>... lists) {
        //    
        final List<Iterator<E>> retlist = new ArrayList<Iterator<E>>();

        //
        for (List<E> list : lists) {
            //
            final ListIterator<E> listIterator = list.listIterator();
            if (listIterator != null) {
                retlist.add(listIterator);
            }
        }

        //
        return retlist.toArray(new ListIterator[retlist.size()]);
    }

    /**
     * Returns an {@link Iterator} instance for the given elements
     * 
     * @param elements
     * @return new instance of an {@link Iterator}
     */
    public static <E> Iterator<E> valueOf(E... elements) {
        return Arrays.asList(elements).iterator();
    }

    /**
     * Resolves the size of a given {@link Iterator} by iterating over it.
     * 
     * @param iterator
     * @return
     */
    public static int size(Iterator<?> iterator) {
        //
        int retval = 0;

        //
        if (iterator != null) {
            for (; iterator.hasNext(); iterator.next()) {
                retval++;
            }
        }

        //
        return retval;
    }
}

Related

  1. toIntegerArray(List list)
  2. toLowerCase(List list)
  3. toLowerCase(List stringList)
  4. toposort(Iterable items, Comparator partOrder)
  5. trimTail(List l)
  6. walkTo(String target, String path, int offset)