Round 1 feedback
Paul Sandoz
paul.sandoz at oracle.com
Thu Feb 14 02:31:51 PST 2013
Here is an alternative using Collectors, which is less general than flat map but achieves the same thing for a terminal flat map->collect->toList pattern.
Not generally recommending this approach. It's useful for educational purposes but could be useful to make things more concise for certain common patterns.
Paul.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Supplier;
import java.util.stream.Collector;
public class Test {
public static void main(String[] args) {
List<List<List<String>>> llls = Arrays.asList(
Arrays.asList(
Arrays.asList("1-1"),
Arrays.asList("1-2"),
Arrays.asList("1-3")
),
Arrays.asList(
Arrays.asList("2-1"),
Arrays.asList("2-2"),
Arrays.asList("2-3")
),
Arrays.asList(
Arrays.asList("3-1"),
Arrays.asList("3-2"),
Arrays.asList("3-3")));
List<String> ls = llls.stream().collect(toFlatList());
// Could also do
// List<String> ls = llls.stream().collect(ArrayList::new, (r, t) -> t.forEach(r::addAll), List::addAll);
System.out.println(ls);
}
static Collector<List<List<String>>, List<String>> toFlatList() {
return new FlatListCollector();
}
static class FlatListCollector implements Collector<List<List<String>>, List<String>> {
@Override
public Supplier<List<String>> resultSupplier() {
return ArrayList::new;
}
@Override
public BiConsumer<List<String>, List<List<String>>> accumulator() {
return (r, t) -> t.forEach(r::addAll);
}
@Override
public BinaryOperator<List<String>> combiner() {
return (l, r)-> { l.addAll(r); return l; };
}
}
}
On Feb 14, 2013, at 9:20 AM, Arul Dhesiaseelan <aruld at acm.org> wrote:
> With b77, you can make this pretty :-)
>
> fooBySomething.values().stream().flatMap(Collection::stream).collectUnordered(toList());
>
> -Arul
>
>
> On Wed, Feb 13, 2013 at 6:32 PM, Michael Nascimento <misterm at gmail.com>wrote:
>
>> On Wed, Jan 9, 2013 at 2:13 PM, Brian Goetz <brian.goetz at oracle.com>
>> wrote:
>>> What we did in 8 is add {Map,ConcurrentMap}.computeIfAbsent. This
>>> eliminates 80% of the pain of simulating a multimap with a
>>> Map<K,Collection<V>>. To wit:
>>>
>>> void add(K k, V v) {
>>> map.computeIfAbsent(k, ArrayList::new).add(v);
>>> }
>>>
>>> This is only slightly worse than
>>> multimap.add(k, v);
>>>
>>> and certainly way less pain than doing it today.
>>>
>>> With this tweak, I think the argument in favor of adding Multimap to the
>> JDK
>>> loses about 80% of its steam.
>>
>> Ok, now to a different point of the Multimap discussion. Turning this:
>>
>> Collection<Foo<Bar>> values = fooBySomething.values();
>>
>> into this:
>>
>> Collection<Foo<Bar>> values =
>> fooBySomething.values().stream().<Foo<Bar>>explode((d, foos) ->
>> d.send(foos)).collectUnordered(toList());
>>
>> is pretty ugly, isn't it? Or is there a better way?
>>
>> Regards,
>> Michael
>>
>>
>
More information about the lambda-dev
mailing list