BISHI23 小红书推荐系统

📅 发布时间:2026/7/9 4:28:45 👁️ 浏览次数:
BISHI23 小红书推荐系统
求解代码publicstaticvoidmain(String[]args)throwsIOException{BufferedReaderbrnewBufferedReader(newInputStreamReader(System.in));PrintWriteroutnewPrintWriter(newOutputStreamWriter(System.out));Stringstrbr.readLine();String[]wordStringsstr.split(\\s);MapString,IntegerhMapnewHashMap();for(Stringword:wordStrings){hMap.put(word,hMap.getOrDefault(word,0)1);}// Map.EntryString, Integer表示Map中的一个键值对包含key和valueListMap.EntryString,IntegerkeywordsnewArrayList();for(Map.EntryString,Integerentry:hMap.entrySet()){// 只保留出现次数≥3的单词if(entry.getValue()3){keywords.add(entry);}}keywords.sort((o1,o2)-{if(!o1.getValue().equals(o2.getValue())){returno2.getValue()-o1.getValue();// 降序}else{returno1.getKey().compareTo(o2.getKey());// 升序}});for(Map.EntryString,Integerentry:keywords){out.println(entry.getKey());}out.flush();out.close();br.close();}小贴士hMap.entrySet()返回一个包含所有 Entry 的集合SetMap.EntryEntry是Map中键值对的最小封装单元一个 Entry 对象 一个 key 一个对应的 value。