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

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

Java:使用Stream API在嵌套列表中查找常見項(xiàng)目

Java:使用Stream API在嵌套列表中查找常見項(xiàng)目

素胚勾勒不出你 2021-05-06 21:20:59
假設(shè)我有一個(gè)List<List<Animal>> animals。此嵌套列表表示一個(gè)位置列表,其中每個(gè)位置都包含動(dòng)物列表。我需要找出至少出現(xiàn)在兩個(gè)不同位置的動(dòng)物類型列表。我知道我可以進(jìn)行常規(guī)循環(huán)并執(zhí)行該操作。有什么方法可以通過Stream API完成嗎?例子:List<List<Animal>> animals = new ArrayList<>();animals.add(Arrays.asList(new Dog(), new Cat()));animals.add(Arrays.asList(new Dog(), new Bird()));animals.add(Arrays.asList(new Bird()));預(yù)期(等同于):List<Class<? extends Animal>> animalTypes = Arrays.asList(Dog.class, Bird.class);至于嘗試,我只設(shè)法將內(nèi)部列表轉(zhuǎn)換為一組類:animals.stream().map(place -> place.stream().map(animal -> animal.getClass()).collect(Collectors.toSet()));更新在沒有Stream API的情況下執(zhí)行此操作的代碼:final List<List<Animal>> animals = new ArrayList<>();animals.add(Arrays.asList(new Dog(), new Cat()));animals.add(Arrays.asList(new Dog(), new Bird()));animals.add(Arrays.asList(new Bird()));final Map<Class<? extends Animal>, Integer> count = new HashMap<>();for (final List<Animal> place : animals) {    final Set<Class<? extends Animal>> uniqueTypes = new HashSet<>();    for (final Animal animal : place) {        uniqueTypes.add(animal.getClass());    }    for (final Class<? extends Animal> type : uniqueTypes) {        if (!count.containsKey(type))        {            count.put(type, 1);        }        else        {            count.put(type, count.get(type).intValue() + 1);        }    }}final List<Class<? extends Animal>> typesAppearingAtLeastAtTwoPlaces = new ArrayList<>();for (final Class<? extends Animal> type : count.keySet()) {    if (count.get(type).intValue() >= 2) {        typesAppearingAtLeastAtTwoPlaces.add(type);    }}System.out.println(typesAppearingAtLeastAtTwoPlaces);輸出:[class Test$Dog, class Test$Bird]
查看完整描述

3 回答

?
繁星coding

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

首先,對(duì)所有動(dòng)物進(jìn)行計(jì)數(shù),然后選擇出現(xiàn)多次的動(dòng)物:


import static java.util.stream.Collectors.*;

.....


Map<Class<? extends Animal>, Long> animalCounts = animals.stream()

        .flatMap(

                lst -> lst.stream()

                    .map(a -> a.getClass())

                    .distinct()   // in case several of the same animal are in the same place

        )

        .collect(groupingBy(x -> x, counting()));


List<Class<? extends Animal>> animalTypes = animalCounts.entrySet().stream()

        .filter(e -> e.getValue() > 1)

        .map(Map.Entry::getKey)

        .collect(toList());


查看完整回答
反對(duì) 回復(fù) 2021-05-12
?
萬千封印

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

我認(rèn)為您也可以嘗試StreamEx。它使您有機(jī)會(huì)編寫更簡潔,更易讀的代碼:

StreamEx.of(animals)
    .flatMap(e -> e.stream().map(Animal::getClass).distinct())
    .distinct(2).toList();


查看完整回答
反對(duì) 回復(fù) 2021-05-12
?
九州編程

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

首先,也許您應(yīng)該嘗試使用flatMap而不是map。


animals.stream()。map(place-> place.stream()。map(animal-> animal.getClass())。collect(Collectors.toSet()));


其次,實(shí)際上我們可以使用外部ConcurrentHashMap做到這一點(diǎn),這將使我們能夠parallel在需要時(shí)使用。


    ConcurrentHashMap<Class, AtomicLong> theCounterMap = new ConcurrentHashMap<>();

    animals.stream().flatMap(list -> list.stream().map(animal -> animal.getClass()).distinct())

        .forEach(clazz -> theCounterMap.computeIfAbsent(clazz, k -> new AtomicLong()).getAndIncrement());

    List<Class> classList = theCounterMap.entrySet().stream()

            .filter(entry -> entry.getValue().get() > 1)

            .map(Map.Entry::getKey)

            .collect(Collectors.toList());

但是,如果您需要跟蹤源列表(作為兩個(gè)不同的位置),則需要進(jìn)一步修改上面的解決方案。


更新

根據(jù)@shmosel的建議,您可以直接使用一種更簡單的方法來實(shí)現(xiàn)相同的目標(biāo),如下所示:


    Map<Class, Long> theCounterMap = animals.stream().flatMap(list -> list.stream().map(animal -> animal.getClass()).distinct())

        .collect(Collectors.groupingBy(e -> e, Collectors.counting()));

    List<Class> classList = theCounterMap.entrySet().stream()

            .filter(entry -> entry.getValue() > 1)

            .map(Map.Entry::getKey)

            .collect(Collectors.toList());


查看完整回答
反對(duì) 回復(fù) 2021-05-12
  • 3 回答
  • 0 關(guān)注
  • 255 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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