题解 | 小红书推荐系统
小红书推荐系统
https://www.nowcoder.com/practice/e5b39c9034a84bf2a5e026b2b9b973d0
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
Map<String, Integer> wordCount = new HashMap<>();
String[] words = input.split("\\s+");
for (String word : words) {
if (!word.isEmpty()) {
wordCount.put(word, wordCount.getOrDefault(word, 0) + 1);
}
}
List<Map.Entry<String, Integer>> keywords = wordCount.entrySet().stream()
.filter(entry -> entry.getValue() >= 3)
.sorted((a, b) -> {
if (!a.getValue().equals(b.getValue())) {
return b.getValue() - a.getValue();
}
return a.getKey().compareTo(b.getKey());
})
.collect(Collectors.toList());
for (Map.Entry<String, Integer> entry : keywords) {
System.out.println(entry.getKey());
}
}
}
