第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Java Streams 按屬性將對象列表組合在一起

使用 Java Streams 按屬性將對象列表組合在一起

嗶嗶one 2022-03-10 21:55:21
我有一個 SensorSample POJO 列表public class SensorSample {    private Device.SensorType sensorType; // This is an enum    private double sample;    private long timestamp;    // Constructor    // Setters    // Getters}我需要按 對它們進(jìn)行分組timestamp,以便SensorSample同一天的所有 s 都在一起。然后我需要減少它們,這樣我SensorSample每天只有一個,它的值是當(dāng)天所有對象sample的值的平均值。sample有沒有辦法用 Streams 做到這一點?到目前為止,我將它們組合在一起:Map<Long, List<SensorSample>> yearSamples = samples.stream()                .collect(groupingBy(sample -> SECONDS_IN_A_DAY*Math.floorDiv(sample.getTimestamp(), SECONDS_IN_A_DAY)));但我不知道如何更進(jìn)一步。
查看完整描述

2 回答

?
慕桂英4014372

TA貢獻(xiàn)1871條經(jīng)驗 獲得超13個贊

像這樣的東西,我想。要查找組的平均數(shù):


Map<Long, Double> averages = samples.stream()

  .collect(groupingBy(SensorSample::getTimestamp,

   averagingDouble(SensorSample::getSample)));

我當(dāng)天沒有擴(kuò)展您的公式,我認(rèn)為如果我只是打電話getTimestamp并省略詳細(xì)信息,它會更具可讀性。如果您將getDay方法添加到SensorSample.


如果您提供了 MCVE,這也將更容易測試,因為只有一個部分類可以繼續(xù)測試上面的代碼有點困難。


查看完整回答
反對 回復(fù) 2022-03-10
?
www說

TA貢獻(xiàn)1775條經(jīng)驗 獲得超8個贊

結(jié)果似乎您想要 aList<SensorSample>之后的每個組groupingBy都減少為單個SensorSample.


List<SensorSample> result = samples.stream()

                .collect(groupingBy(sample -> SECONDS_IN_A_DAY*Math.floorDiv(sample.getTimestamp(), SECONDS_IN_A_DAY))

                .entrySet()

                .stream()

                .map(e -> {

                    SensorSample sensorSample = new SensorSample();

                    sensorSample.setTimestamp(e.getKey());

                    double average = e.getValue().stream()

                            .mapToDouble(SensorSample::getSample)

                            .average().orElse(0);

                    sensorSample.setSample(average);

                    sensorSample.setSensorType(e.getValue().get(0).getSensorType());

                    return sensorSample;

                }).collect(Collectors.toList());

map邏輯似乎有點大,因此我會考慮將其重構(gòu)為這樣的方法:


private static SensorSample apply(Map.Entry<Long, List<SensorSample>> e) {

        SensorSample sensorSample = new SensorSample();

        sensorSample.setTimestamp(e.getKey());

        double average = e.getValue().stream()

                .mapToDouble(SensorSample::getSample)

                .average().orElse(0);

        sensorSample.setSample(average);

        sensorSample.setSensorType(e.getValue().get(0).getSensorType());

        return sensorSample;

}

然后流管道將變?yōu)椋?/p>


List<SensorSample> result = samples.stream()

                .collect(groupingBy(sample -> SECONDS_IN_A_DAY*Math.floorDiv(sample.getTimestamp(), SECONDS_IN_A_DAY))

                .entrySet()

                .stream()

                .map(Main::apply)

                .collect(Collectors.toList());

Main包含該apply方法的類在哪里。


查看完整回答
反對 回復(fù) 2022-03-10
  • 2 回答
  • 0 關(guān)注
  • 155 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號