Here is your answer:
String text = "Very very very good text to compare good text with cats, cats and dogs. " +
"Very good dogs.";
int mostFrequentWordsNumber = 5;
Map<String, Integer> mapOfFrequentWords = new TreeMap<>();
String[] words = text.split("\s+");
for (String word : words) {
if (!mapOfFrequentWords.containsKey(word)) {
mapOfFrequentWords.put(word, 1);
} else {
mapOfFrequentWords.put(word, mapOfFrequentWords.get(word) + 1);
}
}
Map<String, Integer> sorted = mapOfFrequentWords
.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.limit(mostFrequentWordsNumber)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2,
LinkedHashMap::new));
System.out.println(sorted);
Result will be: {good=3, Very=2, dogs.=2, text=2, very=2}
. You can change it to case insensitive to get rid of Very and very as different words.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…