Java 词频统计
来源:互联网 发布:php查询降序排序 编辑:程序博客网 时间:2024/05/21 10:52
import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;import java.util.Map;import java.util.Map.Entry;import java.util.TreeMap;public class WordCount {public static void main(String [] args) throws IOException{BufferedReader br = new BufferedReader(new FileReader("D:/bbb.txt"));List<String> word = new ArrayList<String>();String read = null;while((read = br.readLine())!=null){ String[] wordsArr1 = read.split("[^a-zA-Z]"); for(String words : wordsArr1) word.add(words);}br.close();Map<String, Integer> wordsCount = new TreeMap<String,Integer>(); for (String li : word) { if(wordsCount.get(li) != null){ wordsCount.put(li,wordsCount.get(li) + 1); }else{ wordsCount.put(li,1); } } SortMap(wordsCount);} public static void SortMap(Map<String,Integer> oldmap){ ArrayList<Map.Entry<String,Integer>> list = new ArrayList<Map.Entry<String,Integer>>(oldmap.entrySet()); Collections.sort(list,new Comparator<Map.Entry<String,Integer>>(){ @Override public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) { return o2.getValue() - o1.getValue(); //降序 } }); for(int i = 0; i<list.size(); i++){ System.out.println(list.get(i).getKey()+ ": " +list.get(i).getValue()); } } }
阅读全文
0 0
- java 英文词频统计
- java词频统计
- Java 词频统计
- 【java】以词频升序统计文本词频
- 使用JAVA进行词频统计
- Java 用hashmap统计词频
- java实现词频统计程序
- 词频统计
- 词频统计
- 词频统计
- 词频统计
- 词频统计
- 词频统计
- 词频统计
- 词频统计
- 词频统计
- 词频统计
- 词频统计
- Unity编辑器界面模糊
- Vue绑定class
- springmvc 获取当前请求的 原生request/response
- qwewqewqe
- 系统吞吐量(TPS)、用户并发量、性能测试概念和公式
- Java 词频统计
- python迭代器,生成器
- java初学2
- C++编程中常见的线程和内存问题
- ios 图片尺寸设计和原理
- 接地气又好用,8 款优秀的国产开源博客系统推荐
- Fiddler4入门篇--1(Fiddler的简介以及简单使用)
- python性能测试之 gevent 效果不错
- Ubuntu 14.04 出现 DNS无法解析的问题