Convert array and Iterator as List - Java Collection Framework

Java examples for Collection Framework:Array Convert

Description

Convert array and Iterator as List

Demo Code


//package com.book2s;
import java.util.ArrayList;

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

public class Main {
    public static <E> List<E> asList(
            @SuppressWarnings("unchecked") final E... array) {
        List<E> list = new ArrayList<>(array.length);
        for (E element : array) {
            list.add(element);/*from  www  .  j a v a  2 s .  co  m*/
        }
        return list;
    }

    public static <E> List<E> asList(final Iterator<? extends E> iterator) {
        List<E> list = new ArrayList<>();
        while (iterator.hasNext()) {
            list.add(iterator.next());
        }
        return list;
    }
}

Related Tutorials