作為前言,對于 Java 來說非常新,對于HashMaps
.
我有一個HashMap
字串作為鍵和代表頻率的相關整數值。我想知道找到與最大整數值關聯的鍵的最有效方法。
這是我到目前為止的代碼:
public String most(String[] sentences) {
HashMap<String, Integer> wordFreq = new HashMap<String, Integer>();
for(String sentence : sentences) {
String[] words = sentence.split(" ");
for (String word : words) {
wordFreq.merge(word, 1, Integer::sum);
}
}
//TODO find key associated with max integer value in map
}
uj5u.com熱心網友回復:
你可以這樣做。映射流entrySet
,然后通過使用條目maxBy
的值來獲取最大條目。由于maxBy
回傳 an Optional
,因此您可以使用它map
來獲取值。如果不存在值,則回傳資訊性訊息。
String result = wordFreq.entrySet().stream()
.collect(Collectors.maxBy(Entry.comparingByValue()))
.map(Entry::getKey).orElse("No Value Found");
正如所評論的那樣,您也可以這樣做。出于習慣,我傾向于使用第一個。
String result = wordFreq.entrySet().stream()
.max(Entry.comparingByValue())
.map(Entry::getKey).orElse("No Value Found");
您也可以使用其他方法來優化它,但由于您構建了一張地圖,我認為這就是您想要的方式。如果您需要單詞映射,您可能希望在方法之外構建它。然后只需將映射傳遞給方法而不是句子陣列。這樣您就可以將地圖用于其他用途。
由于您還沒有機會回答我的問題,因此我將提供一個關于單詞最多出現次數的關系的解決方案。與以前類似,除了首先回傳找到的最大值。您可以流式傳輸entrySet
,過濾具有最大值的條目。然后只需收集到一個串列中。
int max = wordFreq.entrySet().stream()
.max(Entry.comparingByValue()).map(Entry::getValue)
.orElse(0);
List<String> wordFreq.entrySet().stream()
.filter(e -> e.getValue() == max).map(Entry::getKey)
.toList();
}
最后,您使用流構造來創建初始頻率圖。我使用\\s
正則運算式,因為可能有多個空格。將splitAsStream
應用該模式,并且與功能toMap
中的 a 類似merge
。
Map<String, Integer> wordFreq = Arrays.stream(sentences)
.flatMap(s -> Pattern.compile("\\s ").splitAsStream(s))
.collect(Collectors.toMap(x -> x, x -> 1,
Integer::sum));
uj5u.com熱心網友回復:
在積累 Map 的值時可以找到最常用的詞。這將允許避免迭代映射條目的開銷。
為此,我們需要在現有代碼中引入幾個變數一個條件。
方法merge()
回傳與提供的鍵關聯的當前值。這允許跟蹤最大值并相應地更新輸入中最常見的單詞。
public String most(String[] sentences) {
Map<String, Integer> wordFreq = new HashMap<>();
String most = "no data";
int max = 0;
for(String sentence : sentences) {
String[] words = sentence.split(" ");
for (String word : words) {
int current = wordFreq.merge(word, 1, Integer::sum);
if (current > max) {
most = word;
max = current;
}
}
}
return most;
}
如果輸入中有多個具有相同頻率的單詞(恰好是最大的)并且您想要獲取所有單詞,則回傳型別應該是字串的集合,例如List<String>
. 我們需要維護一個字串的集合,而不是將最常用的單詞保存為字串。
public List<String> most(String[] sentences) {
Map<String, Integer> wordFreq = new HashMap<>();
List<String> mostFrequent = new ArrayList<>();
int max = 0;
for(String sentence : sentences) {
String[] words = sentence.split(" ");
for (String word : words) {
int current = wordFreq.merge(word, 1, Integer::sum);
if (current >= max) {
if (current > max) {
mostFrequent = new ArrayList<>();
max = current;
}
mostFrequent.add(word);
}
}
}
return mostFrequent;
}
uj5u.com熱心網友回復:
您還可以考慮使用雙向地圖實作,例如apache commons collections BidiMap或guava 的 BiMap
但是由于您的 Map 值不是唯一的,因此為了獲得高性能,您需要某種索引來搜索鍵。沒有可以雙向作業的索引,因此實際上您需要 1 個 Map(用于鍵到值)和 1 個 Multimap(用于值到鍵),以及一個在任何突變(插入、upeate、洗掉,...)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/505875.html
下一篇:使用隨機樞軸C實作就地快速排序