Java tutorial
//package com.java2s; /* * Copyright (c) 2013 The Finnish National Board of Education - Opetushallitus * * This program is free software: Licensed under the EUPL, Version 1.1 or - as * soon as they will be approved by the European Commission - subsequent versions * of the EUPL (the "Licence"); * * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: http://www.osor.eu/eupl/ * * This program 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 * European Union Public Licence for more details. */ import com.google.common.base.Function; import java.util.*; public class Main { /** * @param collection to collect extracted values from * @param extractor to transform items of collection to collections to combine * @param <E> the original container type * @param <T> the extracted item type * @return the extracted items in a single list */ public static <E, T, C extends Collection<T>> List<T> collect(Collection<E> collection, Function<E, C> extractor) { List<T> results = new ArrayList<T>(); for (E item : collection) { results.addAll(extractor.apply(item)); } return results; } /** * @param map to collect * @param keys to collect from map * @param to add each values for given keys from given map * @param <K> key type * @param <T> value type * @param <C> collectin type to collect result to * @return the provided to collection with given keys from map appended */ public static <K, T, C extends Collection<? super T>> C collect(Map<K, T> map, Collection<K> keys, C to) { for (K key : keys) { to.add(map.get(key)); } return to; } }