简体   繁体   English

Java 8流,groupBy和方法调用

[英]Java 8 stream, groupBy and method invocation

I have the following code: 我有以下代码:

ConcurrentMap<String, Zipper> zippers = list.parallelStream()
    .map( f -> {return new Facet( f ) ; } )
    .collect( 
        Collectors.groupingByConcurrent( Facet::getZip,
        Collector.of( Zipper::new, 
                  Zipper::accept, 
                  (a,b)-> {a.combine(b); return a; } )
        )) ;

for ( String key: zippers.keySet() )
{
    zippers.get( key ).zip() ;
}

Given that I only need the Zipper objects to invoke the zip() method on them, is there a way to invoke this method as part of the stream just after the creation of each object (and to have these objects thrown away immediately after the zip() method has been invoked on them) rather than first having to create a map? 鉴于我只需要Zipper对象在其上调用zip()方法,是否有一种方法可以在创建每个对象后立即将该方法作为流的一部分进行调用(并在zip之后立即将这些对象丢弃) ()方法已在其上调用),而不是首先必须创建地图?

You probably want the 4-argument Collector#of which uses a finisher. 您可能想要使用整理器的4参数Collector#of器#of。

Note that f -> {return new Facet(f); } 请注意, f -> {return new Facet(f); } f -> {return new Facet(f); } can be written as Facet::new f -> {return new Facet(f); }可以写为Facet::new

ConcurrentMap<String, Zipper> zippers = list.parallelStream()
    .map(Facet::new)
    .collect( 
        Collectors.groupingByConcurrent(
            Facet::getZip,
            Collector.of( Zipper::new, 
                Zipper::accept, 
                (a,b)-> {a.combine(b); return a; },
                z -> {z.zip(); return z;}
            )
        )
    );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM