Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.util.Collection;

import java.util.function.IntFunction;

public class Main {
    /**
     * Converts the <code>collection</code> into an array
     * using the <code>generator</code> to create the array
     * without creating a <code>Stream</code> on the collection.
     *
     * @param collection The collection to be converted
     * @param generator The generator to create the empty array
     * @param <T> The type of the elements
     *
     * @return An array with the length of the <code>collection</code>
     * containing all elements of the <code>collection</code>
     *
     * @throws NullPointerException if either <code>collection</code>
     * or <code>generator</code> is <code>null</code>
     */
    public static <T> T[] toArray(Collection<T> collection, IntFunction<T[]> generator)
            throws NullPointerException {
        return collection.toArray(generator.apply(collection.size()));
    }
}