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.Function;

public class Main {
    /**
     * Extract attribute from objects in collection and store them in array and
     * return.
     * 
     * @param entities
     * @param keyMapper
     * @param arr
     * @return
     */
    public static <T, K> K[] toArray(Collection<T> entities, Function<T, K> keyMapper, K[] arr) {
        return entities.stream().map(keyMapper).toArray(size -> arr);
    }

    public static int[] toArray(Collection<Integer> col) {
        if (col == null)
            return null;
        int[] arr = new int[col.size()];
        int i = 0;
        for (Integer v : col)
            arr[i++] = v.intValue();
        return arr;
    }
}