Java tutorial
//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 set with elements being results of mapping * the function over the original set. * @param xs original set * @param f function * @param <T> original set element type, also function parameter type * @param <U> function result type * @return set of results of mapping the function */ @Nonnull public static <T, U> Set<U> setMap(@Nonnull Set<T> xs, @Nonnull Function<T, U> f) { HashSet<U> result = new HashSet<U>(xs.size()); for (T x : xs) { result.add(f.apply(x)); } return result; } }