/** * Returns a {@link Map} where elements of this stream with the same key are * grouped together. The resulting {@code Map} keys are the keys of this * stream entries and the values are the collections of the corresponding * values. The collections are created by the provided factory. * * <p> * This is a <a href="package-summary.html#StreamOps">terminal</a> * operation. * * @param <C> the type of the resulting {@code Collection} * @param <M> the type of the resulting {@code Map} * @param mapSupplier a function which returns a new, empty {@code Map} into * which the results will be inserted * @param collectionFactory a {@code Supplier} which returns a new, empty * {@code Collection} of the appropriate type * @return a {@code Map} containing the elements of this stream * @see Collectors#toCollection(Supplier) */ public <C extends Collection<V>, M extends Map<K, C>> M groupingTo(Supplier<M> mapSupplier, Supplier<C> collectionFactory) { return grouping(mapSupplier, Collectors.toCollection(collectionFactory)); }
/** * Returns a {@link Map} where elements of this stream with the same key are * grouped together. The resulting {@code Map} keys are the keys of this * stream entries and the values are the lists of the corresponding values. * * <p> * There are no guarantees on the type, mutability, serializability, or * thread-safety of the {@code Map} or {@code List} objects returned. If * more control over the returned {@code Map} is required, use * {@link #grouping(Supplier)}. If more control over the lists required, use * {@link #groupingTo(Supplier)}. * * <p> * This is a <a href="package-summary.html#StreamOps">terminal</a> * operation. * * @return a {@code Map} containing the elements of this stream * @see Collectors#groupingBy(Function) */ public Map<K, List<V>> grouping() { return grouping(Collectors.toList()); }
/** * Returns a {@link Map} where elements of this stream with the same key are * grouped together. The resulting {@code Map} keys are the keys of this * stream entries and the values are the lists of the corresponding values. * The {@code Map} is created using the provided supplier function. * * <p> * There are no guarantees on the type, mutability, serializability, or * thread-safety of the {@code List} objects returned. If more control over * the lists required, use {@link #groupingTo(Supplier)}. * * <p> * This is a <a href="package-summary.html#StreamOps">terminal</a> * operation. * * @param <M> the type of the resulting {@code Map} * @param mapSupplier a function which returns a new, empty {@code Map} into * which the results will be inserted * @return a {@code Map} containing the elements of this stream * @see #grouping(Supplier, Collector) * @see #groupingTo(Supplier, Supplier) */ public <M extends Map<K, List<V>>> M grouping(Supplier<M> mapSupplier) { return grouping(mapSupplier, Collectors.toList()); }
/** * Returns a {@link Map} where elements of this stream with the same key are * grouped together. The resulting {@code Map} keys are the keys of this * stream entries and the values are the collections of the corresponding * values. The collections are created by the provided factory. * * <p> * There are no guarantees on the type, mutability, serializability, or * thread-safety of the {@code Map} object returned. If more control over * the returned {@code Map} is required, use * {@link #groupingTo(Supplier, Supplier)}. * * <p> * This is a <a href="package-summary.html#StreamOps">terminal</a> * operation. * * @param <C> the type of the resulting {@code Collection} * @param collectionFactory a {@code Supplier} which returns a new, empty * {@code Collection} of the appropriate type * @return a {@code Map} containing the elements of this stream * @see Collectors#toCollection(Supplier) */ public <C extends Collection<V>> Map<K, C> groupingTo(Supplier<C> collectionFactory) { return grouping(Collectors.toCollection(collectionFactory)); }