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));
? ? }
}
