我有一個來自 Type 的 ArrayList Cell
。ArrayList 具有來自 Excel 表格單元格的值。我嘗試了該站點的不同解決方案,但沒有一個是正確的。這就是為什么我沒有代碼向您展示我所做的。幾乎所有的解決方案都是為了String
.
我需要在我的 ArrayList 中獲取最常見的值。
我試圖在解決方案中替換Strings
withcell.getNumericValue()
并更改型別,Double
但它沒有用。
有沒有人解決我的問題?
Map<Cell, Long> f = dataaccx
.stream()
.collect(Collectors.groupingBy(v -> v, Collectors.counting()));
Cell maxOccurence =
Collections.max(f.entrySet(), Comparator.comparing(Map.Entry::getValue)).getKey();
System.out.println(maxOccurence.getNumericCellValue());
這就是我嘗試過的一個例子。dataaccx
是我的 Arraylist。
uj5u.com熱心網友回復:
您應該按單元格的數值而不是物件本身進行分組。
Map<Double, Long> f = cells
.stream()
.collect(Collectors.groupingBy(Cell::getNumericCellValue, Collectors.counting()));
Map.Entry maxOccurence =
Collections.max(f.entrySet(), Comparator.comparing(Map.Entry::getValue));
System.out.println(maxOccurence.getKey());
uj5u.com熱心網友回復:
如果可能有多個最常見的值并且需要列印,那么構建頻率圖并在構建地圖時使用普通回圈在單次運行中找到最大頻率可能會更好。
然后可以應用 Stream API 來列印結果。
int max = 0;
Map<Double, Integer> freqMap = new LinkedHashMap<>();
for (Cell c : cells) {
int freq = freqMap.merge(c.getNumericCellValue(), 1, Integer::sum);
if (freq > max) {
max = freq;
}
}
final int maxFreq = max; // max is not final and cannot be used in lambda below
freqMap.entrySet()
.stream()
.filter(e -> e.getValue() == maxFreq)
.map(Map.Entry::getKey)
.forEach(System.out::println);
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/382214.html
標籤:爪哇 列表 数组列表 收藏 apache-poi