4 回答

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);
}

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]);

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

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
添加回答
舉報(bào)