Java Iterator toStringArray(Iterator iterator)

Here you can find the source of toStringArray(Iterator iterator)

Description

Creates a new array of String objects, containing the elements of a supplied Iterator.

License

Apache License

Parameter

Parameter Description
iterator The iterator containing the elements to create the array with.

Return

The new String array.

Declaration

public static String[] toStringArray(Iterator<String> iterator) 

Method Source Code

//package com.java2s;
/*// w w w  . j a v  a  2 s  .com
 * Copyright 2001-2013 Geert Bevin (gbevin[remove] at uwyn dot com)
 * Licensed under the Apache License, Version 2.0 (the "License")
 */

import java.util.*;

public class Main {
    /**
     * Creates a new array of <code>String</code> objects, containing the
     * elements of a supplied <code>Iterator</code>.
     *
     * @param iterator The iterator containing the elements to create the
     *                 array with.
     * @return The new <code>String</code> array.
     * @since 1.0
     */
    public static String[] toStringArray(Iterator<String> iterator) {
        if (null == iterator) {
            return new String[0];
        }

        ArrayList<String> strings = new ArrayList<>();

        while (iterator.hasNext()) {
            strings.add(iterator.next());
        }

        String[] string_array = new String[strings.size()];
        strings.toArray(string_array);

        return string_array;
    }
}

Related

  1. toMap(final Iterator> iterator)
  2. toObjectIterator(Object[] objects)
  3. toSet(Iterator iteration)
  4. toString(Iterator iterObjects)
  5. toString(Iterator tokens)
  6. unwrapIterator(Iterator iter)