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

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

如何使用 lambda 從列表中查找最大數(shù)字及其出現(xiàn)次數(shù)?

如何使用 lambda 從列表中查找最大數(shù)字及其出現(xiàn)次數(shù)?

慕絲7291255 2022-09-22 19:22:09
請(qǐng)?jiān)谙旅嬲业揭粋€(gè)代碼,該代碼計(jì)算流中數(shù)字的出現(xiàn)次數(shù),并返回一個(gè)映射,其中數(shù)字作為鍵,值作為其在流中的出現(xiàn)次數(shù):Map<Integer, Long> collect = Stream.of(1, 2, 3, 4, 4, 55555, 12)                 .collect(groupingBy(Function.identity(), counting()));如何將生成的地圖限制為僅最大數(shù)字(或在出現(xiàn)平局時(shí)為數(shù)字)?
查看完整描述

4 回答

?
慕碼人8056858

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

看看這個(gè)簡(jiǎn)單的例子:


public static void getMeNumbersWithHighestFrequence3(int[] numbers, int howMany) {


    Map<Integer, Long> collect = IntStream.of(numbers).boxed().collect(groupingBy(Function.identity(), TreeMap::new, counting())).descendingMap().entrySet().stream()

            .limit(howMany)

            .collect(TreeMap::new, (map, entry) -> map.put(entry.getKey(), entry.getValue()), Map::putAll);


}

您還可以通過放置過濾器值以某種方式搜索它們,它將采用鍵值大于該值的所有條目,如下所示:


  public static void getMeNumbersWithHighestFrequenceByFilterNumber(int[] numbers, int value) {


        Map<Integer, Long> collect = IntStream.of(numbers).boxed().collect(groupingBy(Function.identity(), TreeMap::new, counting())).descendingMap().headMap(value, true);


    }

用法簡(jiǎn)單:


public static void main(String[] args) {

        int[] numbers = {1, 2, 3, 4, 4, 55555, 12};

        getMeNumbersWithHighestFrequence(numbers, 5);

    }


查看完整回答
反對(duì) 回復(fù) 2022-09-22
?
青春有我

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

我建議您使用StreamEx中定義的算盤或我的庫算盤工具MoreCollectors


int result = Stream.of(1, 2, 12, 3, 4, 4, 55555, 12)

    .collect(MoreCollectors.maxAll(MoreCollectors.countingInt()));


// map result

Map<Integer, Integer> mapResult = Stream.of(1, 2, 12, 3, 4, 4, 55555, 12)

    .collect(maxAll(groupingBy(Function.identity(), countingInt())));

因?yàn)槟憧赡芟胍?)所有最大數(shù)字的總和,或2)將它們映射到其他東西,或3)...更多。不需要也不應(yīng)該只為一個(gè)特定的用戶案例編寫這種特定的代碼。(如果你想知道如何實(shí)現(xiàn)它,只需下載庫的源代碼或反編譯類。它們發(fā)布在阿帕奇許可證 v2 上)


更新。實(shí)際上,如果你在談?wù)摂?shù)字,我認(rèn)為這可能是一個(gè)錯(cuò)誤的問題,因?yàn)閭鹘y(tǒng)的數(shù)字比使用lambdas更簡(jiǎn)單,更有效:for-loop


int[] nums = {1, 2, 12, 3, 4, 4, 55555, 12, 55555};

int[] result = {Integer.MIN_VALUE, 0}; // [0] is the largest number if [1] (occurrence) is bigger than 0.


for (int num : nums) {

  if (num > result[0]) {

    result[0] = num;

    result[1] = 1;

  } else if (num == result[0]) {

    result[1]++;

  }

}


System.out.println(result[0] + ": " + result[1]);

如果您必須使用流/Lambdas:


int[] result = IntStream.of(nums).collect(() -> new int[] {Integer.MIN_VALUE, 0}, (a, num) -> {

  if (num > a[0]) {

    a[0] = num;

    a[1] = 1;

  } else if (num == a[0]) {

    a[1]++;

  }

}, (a1, a2) -> {

  if (a1[0] == a2[0]) {

    a1[1] += a2[1];

  } else if (a1[0] < a2[0]) {

    a1[1] = a2[1];

  }

});


System.out.println(result[0] + ": " + result[1]);


查看完整回答
反對(duì) 回復(fù) 2022-09-22
?
鴻蒙傳說

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

我希望有人能想出一個(gè)更簡(jiǎn)單的解決方案:


 List<Entry<Integer, Long>> list =

        List.of(3, 3, 4, 4, 5, 5, 1)

            .stream()

            .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))

            .entrySet()

            .stream()

            .sorted(Map.Entry.<Integer, Long>comparingByValue().reversed())

            .collect(Collectors.toList());


    Map<Integer, Long> result = new HashMap<>();

    Iterator<Entry<Integer, Long>> iter = list.iterator();

    Entry<Integer, Long> left;

    Entry<Integer, Long> right = null;

    while (iter.hasNext()) {

        left = iter.next();

        if (right == null) {

            result.put(left.getKey(), left.getValue());

        }

        if (iter.hasNext() && (right = iter.next()).getValue().longValue() == left.getValue()) {

            result.put(right.getKey(), right.getValue());

        } else {

             break;

        }

    }

因此,首先將它們收集到您已經(jīng)擁有的同一張地圖上。然后按值對(duì)它們進(jìn)行排序;然后迭代該結(jié)果,并僅獲取那些處于最開始并與其值匹配的結(jié)果。


這個(gè)想法是,由于這些已經(jīng)按值排序: - 我們只需要迭代,只要重復(fù),只要沒有這樣的匹配,我們就完成了(因?yàn)樗鼈兪桥判虻?,因此任何后面的元素只?huì)更?。? = 2; 4 = 2; 5 = 2; 1 = 12!= 2


查看完整回答
反對(duì) 回復(fù) 2022-09-22
?
慕婉清6462132

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

我假設(shè)您希望獲得流中最常見的數(shù)字。因此,您可以使用樹狀圖來收集所有結(jié)果并獲取最后一個(gè)條目的列表:

Map<Integer, Long> collect = Stream.of(1, 2, 3, 4, 4, 55555, 12)
        .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
        .entrySet().stream()
        .collect(Collectors.groupingBy(Map.Entry::getValue, TreeMap::new, Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)))
        .lastEntry().getValue();

這將返回包含數(shù)字和頻率的條目列表。在您的情況下,它打印:

{4=2}

如果您只想獲得最大數(shù)字的出現(xiàn)次數(shù),則可以使用以下命令:

Map.Entry<Integer, Long> collect = Stream.of(1, 2, 3, 4, 4, 55555, 12)
        .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
        .entrySet().stream()
        .max(Map.Entry.comparingByKey())
        .orElse(null);

哪些打?。?/p>

55555=1

在最后一種情況下,您只有一個(gè)(最大值)返回條目。也可以使用一個(gè)獲取最大值,應(yīng)該有更好的性能。TreeMap


查看完整回答
反對(duì) 回復(fù) 2022-09-22
  • 4 回答
  • 0 關(guān)注
  • 214 瀏覽

添加回答

舉報(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)