我有一个英文的文本,已经存为了String[],现在想统计这个文本的词频,并且从高到底排序,应该如何写?
解决方案 »
- 求 extjs 表单布局设计器
- 制作网站时,jsp突然创建不了servlet了,哪位大侠有过此经验?
- 我的Tomcat的80端口被占用了,请问我如何察看该端口被什莫程序占用了
- 紧急求救,那里有java.SQL.* 类的说明和使用方法?
- 关于对象数组的问题
- jboss-eap-6.1访问部署应用问题
- 关于HttpServletRequest的问题
- 用JB8编写JSP程序时,怎么老是出现这个问题,帮看一下?
- 请教:如何实现JSP系统的加密。
- 我找了个JDK。1。4的src.zip放在我的jdk1.3的jre\lib\ext,想在JB7里用javax.sql.*但有问题?
- JSP 更新 MySQL 语句时遇到错误了= =
- 获取session的问题 听说标题要长才会有人来回答 所以呢。。。
public static void sort(String[] str){
for(int i=0;i<str.length-1;i++){
String maxStr = str[i];
int index = i;
for(int j=i+1;j<str.length;j++){
if(maxStr.compareTo(str[j]) < 0){
maxStr = str[j];
index = j;
}
}
str[index] = str[i];
str[i] = maxStr;
}
}写个main方法调用一下就可以!
public class Test { public static void main(String[] args) {
String[] strs = new String[]{"a","b","a"};
Map<String, Integer> map = new HashMap<String, Integer>();
for(String s : strs){
Integer key = map.get(s);
if(key == null){
map.put(s, 1);
}else{
map.put(s, key.intValue() + 1);
}
}
Set<Entry<String, Integer>> set = map.entrySet();
List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>();
Iterator<Entry<String, Integer>> iter = set.iterator();
while(iter.hasNext()){
list.add(iter.next());
}
Collections.sort(list, new ComparatorEntry());
for(Entry<String, Integer> entry : list){
System.out.println(entry.getKey() + " : " + entry.getValue());
}
}
}class ComparatorEntry implements Comparator<Entry<String, Integer>>{
@Override
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
return o2.getValue().intValue() - o1.getValue().intValue();
}
}
我还有个问题,最后的结果是存在了list中,对吧?那如果我想去list中词频最高的20个再对其词频进行计算,我要怎么写呢?