How to force max to return ALL maximum values in a Java Stream?

后端 未结 7 961
陌清茗
陌清茗 2020-11-22 09:47

I\'ve tested a bit the max function on Java 8 lambdas and streams, and it seems that in case max is executed, even if more than one object compares

7条回答
  •  生来不讨喜
    2020-11-22 10:12

    System.out.println(
      Stream.of(1, 3, 5, 3, 2, 3, 5)
        .map(a->new Integer[]{a})
        .reduce((a,b)-> 
            a[0]==b[0]?
                Stream.concat(Stream.of(a),Stream.of(b)).toArray() :
                a[0]>b[0]? a:b
        ).get()
    )
    

提交回复
热议问题