How to calculate the average of a Project Reactor Flux?

前端 未结 2 517
小蘑菇
小蘑菇 2021-01-29 03:43

I have a method that returns a Flux, let\'s suppose SensorData has a field measure: Integer.

I would like to calculate the av

相关标签:
2条回答
  • 2021-01-29 03:53

    Another way of doing it with an auxiliary object:

    val average = sensorFlux.map { it.measure }
                    .map { Measure(1, it) }
                    .reduce { t: Measure, u: Measure -> Measure(t.elements + u.elements, t.sum + u.sum) }
                    .map { it.sum / it.elements }
    
    ---
    
    data class Measure(var elements: Int, var sum: Long)
    
    0 讨论(0)
  • 2021-01-29 03:57
    Mono<Double> average = sensorFlux.collect(Collectors.averagingInt(SensorData::getMeasure))
    
    0 讨论(0)
提交回复
热议问题