面試題-排序:統(tǒng)計(jì)一篇文章中單詞出現(xiàn)的次數(shù)

package?example;

import?java.util.*;

/**
?*?Created?by?TR_VMHyper?on?2016/8/17.
?*/
public?class?Test?{
? ? public?static?void?main(String[]args){
? ? ? ? /**
? ? ? ? ?*?面試題:統(tǒng)計(jì)一篇文章中出現(xiàn)單詞出現(xiàn)的次數(shù)
? ? ? ? ?*/
? ? ? ? String?str="Do?be?a?good?student?and?also?to?be?a?good?boy?.?The?beautiful?girl?i?hope?to?marry?with?you?.?It's?my?dream.?Dream?for?dream?...";
? ? ? ? String[]sts=str.split("?");
? ? ? ? Map<String,Integer>?map=new?HashMap<String,Integer>();
? ? ? ? for?(String?s?:?sts)?{
? ? ? ? ? ? if?(map.containsKey(s.toLowerCase())){
? ? ? ? ? ? ? ? map.put(s.toLowerCase(),map.get(s.toLowerCase())+1);
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? map.put(s.toLowerCase(),1);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? int?max=0;
? ? ? ? //System.out.println(map.entrySet());
? ? ? ? Set<Map.Entry<String,Integer>>ks=?map.entrySet();
? ? ? ? List<Map.Entry<String,Integer>>ins=new?ArrayList<Map.Entry<String,Integer>>(ks);
? ? ? ? Collections.sort(ins,?new?Comparator<Map.Entry<String,Integer>>()?{
? ? ? ? ? ? @Override
? ? ? ? ? ? public?int?compare(Map.Entry<String,?Integer>?o1,?Map.Entry<String,?Integer>?o2)?{
? ? ? ? ? ? ? ? if?(o1.getValue()<o2.getValue()){
? ? ? ? ? ? ? ? ? ? return?-1;
? ? ? ? ? ? ? ? }else?if?(o1.getValue()>o2.getValue()){
? ? ? ? ? ? ? ? ? ? return?1;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? return?0;
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? System.out.println(ins);
? ? ? ? //System.out.print(ins.get(ins.size()-1));
? ? }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語(yǔ)法,類相關(guān)的語(yǔ)法,內(nèi)部類的語(yǔ)法,繼承相關(guān)的語(yǔ)法,異常的語(yǔ)法,線程的語(yǔ)...
    子非魚_t_閱讀 34,638評(píng)論 18 399
  • 第十天 權(quán)限修飾符 public protected default private 同一類 true true ...
    炙冰閱讀 604評(píng)論 0 1
  • 一、 1、請(qǐng)用Java寫一個(gè)冒泡排序方法 【參考答案】 public static void Bubble(int...
    獨(dú)云閱讀 1,494評(píng)論 0 6
  • java筆記第一天 == 和 equals ==比較的比較的是兩個(gè)變量的值是否相等,對(duì)于引用型變量表示的是兩個(gè)變量...
    jmychou閱讀 1,644評(píng)論 0 3
  • 這是古典老師在“得到”APP上開(kāi)設(shè)的《超級(jí)個(gè)體》專欄的學(xué)習(xí)總結(jié),第一周,主題是“未來(lái)5年的職場(chǎng)趨勢(shì)”。 1.未來(lái)職...
    聽(tīng)早雨閱讀 745評(píng)論 0 1

友情鏈接更多精彩內(nèi)容