List of usage examples for java.util.stream Collectors toCollection
public static <T, C extends Collection<T>> Collector<T, ?, C> toCollection(Supplier<C> collectionFactory)
From source file:Main.java
public static void main(String[] args) { Stream<String> s = Stream.of("a", "b", "c"); Collection<String> names = s.collect(Collectors.toCollection(TreeSet::new)); System.out.println(names);/*w ww.ja va 2 s .c o m*/ }
From source file:Main.java
public static void main(String[] args) { // collect as typed array Stream<String> words = Stream.of("All", "men", "are", "created", "equal"); Set<String> wordsSet = words.collect(Collectors.toCollection(TreeSet::new)); wordsSet.forEach(n -> System.out.println(n)); }
From source file:Main.java
public static void main(String[] args) { SortedSet<String> uniqueSortedNames = Employee.persons().stream().map(Employee::getName) .collect(Collectors.toCollection(TreeSet::new)); System.out.println(uniqueSortedNames); System.out.println(uniqueSortedNames); }
From source file:Main.java
public static <T> Set<T> asSet(T... values) { return Arrays.stream(values).collect(Collectors.toCollection(LinkedHashSet::new)); }
From source file:Main.java
public static <T, K, V> HashMap<K, HashSet<V>> group(Collection<T> entities, Function<T, K> keyMapper, Function<T, V> valueMapper) { return entities.stream().collect(Collectors.groupingBy(keyMapper, HashMap::new, Collectors.mapping(valueMapper, Collectors.toCollection(HashSet::new)))); }
From source file:Main.java
/** * Transform the source collection using the mapping function to convert the elements to the output type. The result * is written to a set.//from ww w.j a v a2s . com * * @param <T> * the source type type * @param <R> * the result type * @param source * the source * @param mapping * the mapping used for the value transformation * @return the sets the */ public static <T, R> Set<R> transformToSet(Collection<T> source, Function<T, R> mapping) { return source.stream().map(mapping) .collect(Collectors.toCollection(() -> createLinkedHashSet(source.size()))); }
From source file:Main.java
/** * Returns a {@code Collector} that accumulates the input elements into a new {@code List}, in encounter order. If * the provided size is greater than zero then {@link ArrayList} with the given size will be created otherwise a * {@link LinkedList} will be created for undermined size. * * @param <T>/*from w ww. j av a2 s . co m*/ * the type of the input elements * @param <C> * the type of the resulting {@code Collection} * @param expectedSize * the expected size of the collection if known. If not known a -1 could be passed. * @return a {@code Collector} which collects all the input elements into a {@code List}, in encounter order */ @SuppressWarnings("unchecked") public static <T, C extends Collection<T>> Collector<T, ?, C> toList(int expectedSize) { if (expectedSize > 0) { return (Collector<T, ?, C>) Collectors.toCollection(() -> new ArrayList<>(expectedSize)); } return (Collector<T, ?, C>) Collectors.toCollection(LinkedList::new); }
From source file:org.bunkr.core.inventory.InventoryJSON.java
@SuppressWarnings("unchecked") public static JSONAware encodeO(Inventory input) { JSONObject out = new JSONObject(); JSONArray files = input.getFiles().stream().map(FileInventoryItemJSON::encodeO) .collect(Collectors.toCollection(JSONArray::new)); out.put("files", files); JSONArray folders = input.getFolders().stream().map(FolderInventoryItemJSON::encodeO) .collect(Collectors.toCollection(JSONArray::new)); out.put("folders", folders); out.put("defaultEncryptionAlgorithm", input.getDefaultEncryption().toString()); return out;/*w ww. jav a 2 s . co m*/ }
From source file:com.yahoo.bard.webservice.util.Utils.java
/** * Given a collection of objects which share the same super class, return the subset of objects that share a common * sub class.// w ww. j a va 2 s.com * * @param set Input set * @param <T> Input set type * @param type sub class * @param <K> sub class type * * @return ordered subset of objects that share a common sub class */ public static <T, K extends T> LinkedHashSet<K> getSubsetByType(Collection<T> set, Class<K> type) { return set.stream().filter(member -> type.isAssignableFrom(member.getClass())) .map(StreamUtils.uncheckedCast(type)).collect(Collectors.toCollection(LinkedHashSet::new)); }
From source file:org.bunkr.core.inventory.FolderInventoryItemJSON.java
@SuppressWarnings("unchecked") public static JSONAware encodeO(FolderInventoryItem input) { JSONObject out = new JSONObject(); out.put("name", input.getName()); out.put("uuid", input.getUuid().toString()); JSONArray files = input.getFiles().stream().map(FileInventoryItemJSON::encodeO) .collect(Collectors.toCollection(JSONArray::new)); out.put("files", files); JSONArray folders = input.getFolders().stream().map(FolderInventoryItemJSON::encodeO) .collect(Collectors.toCollection(JSONArray::new)); out.put("folders", folders); return out;/*ww w . j a v a 2 s .c o m*/ }