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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用 Java 流從列表中的列表中平均特定值

使用 Java 流從列表中的列表中平均特定值

交互式愛(ài)情 2023-06-14 14:25:45
無(wú)法弄清楚如何在特定建筑物的列表中讀出動(dòng)物的平均重量。我寫了另一種方法來(lái)獲取每種動(dòng)物的動(dòng)物名稱,所以我知道我的堅(jiān)持是有效的。出色地:public class Zoo {    private List<Animal> animals;    private List<Building> buildings;    public Zoo() {        this.animals = new PersistencyController().giveAnimals();        this.gebouwen = new PersistencyController().giveBuildings();    } public List<Animal> giveAnimalsByKind(String kindName) {        return animals.stream().filter(animal -> animal.getKind().getName() == kindName).sorted(Comparator.comparing(Animal::getWeight)).collect(Collectors.toList());    }    public double giveAvgWeightForAnimalsInBuilding(String buildingName) {        return animals.stream().collect(Collectors.averagingDouble(Animal::getWeight));    }}動(dòng)物:public class Animal implements Serializable {    private int nr;    private String name;    private double weight;    private Kind kind;    public Animal(int nr, String name, double weight, Kind kind) {        this.nr = nr;        this.name = name;        this.weight= weight;        this.kind= kind;    }    public double getWeight() {        return weight;    }    public void setWeight(double weight) {        this.weight = weight;    }}建筑:public class Building{    private String naam;    private int capaciteit;    private final List<Animal> animals = new ArrayList<>();    public Building(String naam, int capaciteit) {        this.naam = naam;        this.capaciteit = capaciteit;    }}我想獲得每個(gè)建筑物的動(dòng)物的平均重量,所以我的想法是傳遞建筑物的名稱。每個(gè) Building 對(duì)象都存儲(chǔ)一個(gè)動(dòng)物列表,我需要 building 對(duì)象,因?yàn)樗鎯?chǔ)了建筑物的名稱,但我不知道如何使用流訪問(wèn)它。我知道在我發(fā)布的代碼中,我實(shí)際上還沒(méi)有使用 buildingName,但那是因?yàn)槲沂仓翢o(wú)法正常獲得動(dòng)物的平均體重。weight 在 Animal 中存儲(chǔ)為 double 并且我的堅(jiān)持正在發(fā)揮作用,因?yàn)槲乙呀?jīng)以其他方式對(duì)此進(jìn)行了測(cè)試。任何對(duì)流有經(jīng)驗(yàn)的人都將不勝感激,解釋如何通過(guò)構(gòu)建進(jìn)行過(guò)濾并得到這個(gè)平均權(quán)重。
查看完整描述

1 回答

?
至尊寶的傳說(shuō)

TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超10個(gè)贊

如果你有使用 Java9 的自由,我建議你使用flatMapping收集器來(lái)完成這件事。這是它的樣子。


Map<String, Double> avgWeightByBuildingName = buildings.stream()

    .collect(Collectors.groupingBy(Building::getName,

        Collectors.flatMapping(b -> b.getAnimals().stream(), 

            Collectors.averagingDouble(Animal::getWeight))));

然而,這里的 java8 解決方案與前者相比看起來(lái)有點(diǎn)冗長(zhǎng)。


Map<String, Double> avgWeightByBuildingName = buildings.stream()

    .flatMap(b -> b.getAnimals().stream()

        .map(a -> new AbstractMap.SimpleEntry<>(b.getName(), a.getWeight())))

    .collect(Collectors.groupingBy(Map.Entry::getKey, 

        Collectors.averagingDouble(Map.Entry::getValue)));

flatMapping或者考慮按照此答案編寫您自己的收集器。這是我在查看上述答案和 JDK 9 源代碼后想出的一個(gè)這樣的實(shí)現(xiàn)。


static <T, U, A, R> Collector<T, ?, R> flatMapping(Function<? super T,

            ? extends Stream<? extends U>> mapper, Collector<? super U, A, R> downstream) {

    BiConsumer<A, ? super U> downstreamAccumulator = downstream.accumulator();

    return Collector.of(downstream.supplier(), (r, t) -> {

        try (Stream<? extends U> result = mapper.apply(t)) {

            result.sequential().forEach(u -> downstreamAccumulator.accept(r, u));

        }

    }, downstream.combiner(), downstream.finisher(), 

            downstream.characteristics().toArray(new Characteristics[0]));

}

此外,如以下評(píng)論所述,這對(duì)于較新的 Java 版本有一個(gè)直接的遷移策略。


查看完整回答
反對(duì) 回復(fù) 2023-06-14
  • 1 回答
  • 0 關(guān)注
  • 126 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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