【问题标题】:How to use parallel stream in Groovy?如何在 Groovy 中使用并行流?
【发布时间】:2022-10-22 09:04:09
【问题描述】:

我想问如何在 Groovy 中编写以下代码。

List < String > nums = ['a','b']

Map<String, Long> counts = nums.parallelStream()
.collect( Collectors.groupingBy(k -> k.toLowerCase(), 
Collectors.counting()) );          
Output in java: {a:1,b:1}

I tried using the same but it gives error below:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/home/cg/root/492337/main.groovy: 7: unexpected token: -> @ line 7, column 43.
lect( Collectors.groupingBy(k -\> k.toLow

【问题讨论】:

    标签: java dictionary groovy stream


    【解决方案1】:
    import java.util.stream.Collectors
    
    List < String > nums = ['a','b']
    
    Map<String, Long> counts = nums.parallelStream()
      .collect( Collectors.groupingBy( {k -> k.toLowerCase()}, Collectors.counting()) )
    

    【讨论】:

    • 我正在尝试使用 parallelstream() 在此之后创建一个 dups 和 nondups 列表:比如 If arraylist is ['A','a','B','C','C'] then dups:[' A','a','C','C'] 非重复:['B']
    • 到目前为止,我做了: Map<Boolean, List<String>> hasDup = counts.entrySet().parallelStream() .collect(Collectors.partitioningBy( entry -> entry.getValue() > 1, Collectors.mapping(Map.Entry ::getKey, Collectors.toList())));
    • 但这给出了 dup:['a','b'] nondups:['b']。有没有办法获得与第一条评论相同的输出非常感谢您的帮助;)
    • 要获得第一条评论的输出,您必须遍历原始列表并将其与相应的分区进行比较...
    • 所以这可以并行完成。我应该对 2 条评论代码进行哪些更改谢谢:)
    猜你喜欢
    • 2022-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多