package example;
import java.util.*;
/**
* Created by TR_VMHyper on 2016/8/17.
*/
public class Test {
public static void main(String[]args){
/**
* 面试题:统计一篇文章中出现单词出现的次数
*/
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));
}
}
网友评论