Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/* Copyright (c) 2014 Karol Stasiak
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 */

import com.google.common.base.Function;

import java.util.*;
import javax.annotation.Nonnull;

public class Main {
    /**
     * Creates a new list with elements being results of mapping
     * the function over the original list.
     * @param xs original list
     * @param f function
     * @param <T> original list element type, also function parameter type
     * @param <U> function result type
     * @return list of results of mapping the function
     */
    @Nonnull
    public static <T, U> List<U> listMap(@Nonnull List<T> xs, @Nonnull Function<T, U> f) {
        ArrayList<U> result = new ArrayList<U>(xs.size());
        for (T x : xs) {
            result.add(f.apply(x));
        }
        return result;
    }
}