# HG changeset patch # User psandoz # Date 1425027505 -3600 # Node ID a2c7a365dde4088fa1fbd0bcdbb088be1dcdcf3e # Parent 874d76e4699dfcd61ae1826c9fe0ddc1610ad598 8071600: Add a flat-mapping collector Reviewed-by: smarks, chegar, briangoetz diff -r 874d76e4699d -r a2c7a365dde4 jdk/src/java.base/share/classes/java/util/stream/Collectors.java --- a/jdk/src/java.base/share/classes/java/util/stream/Collectors.java Wed Jul 05 20:22:22 2017 +0200 +++ b/jdk/src/java.base/share/classes/java/util/stream/Collectors.java Fri Feb 27 09:58:25 2015 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -404,6 +404,54 @@ } /** + * Adapts a {@code Collector} accepting elements of type {@code U} to one + * accepting elements of type {@code T} by applying a flat mapping function + * to each input element before accumulation. The flat mapping function + * maps an input element to a {@link Stream stream} covering zero or more + * output elements that are then accumulated downstream. Each mapped stream + * is {@link java.util.stream.BaseStream#close() closed} after its contents + * have been placed downstream. (If a mapped stream is {@code null} + * an empty stream is used, instead.) + * + * @apiNote + * The {@code flatMapping()} collectors are most useful when used in a + * multi-level reduction, such as downstream of a {@code groupingBy} or + * {@code partitioningBy}. For example, given a stream of + * {@code Order}, to accumulate the set of line items for each customer: + *
{@code + * Map+ * + * @param> itemsByCustomerName + * = orders.stream().collect(groupingBy(Order::getCustomerName, + * flatMapping(order -> order.getLineItems().stream(), toSet()))); + * }