沂源网站设计,滨江区网站开发公司,盐城永祥建设有限公司网站,wordpress 默认 私密https://blog.csdn.net/seagal890/article/details/92067644JAVA算法#xff1a;按照给定的段落统计单词出现次数(JAVA代码)写一个 JAVA程序以统计一个文本文件 words.txt 中每个单词出现的频率。为了简单起见#xff0c;你可以假设#xff1a;words.txt只包括小写字母和 …https://blog.csdn.net/seagal890/article/details/92067644JAVA算法按照给定的段落统计单词出现次数(JAVA代码)写一个 JAVA程序以统计一个文本文件 words.txt 中每个单词出现的频率。为了简单起见你可以假设words.txt只包括小写字母和 。每个单词只由小写字母组成。单词间由一个或多个空格字符分隔。示例:假设 words.txt 内容如下the day is sunny the thethe sunny is is你的脚本应当输出(以词频降序排列)the 4is 3sunny 2day 1说明:不要担心词频相同的单词的排序问题每个单词出现的频率都是唯一的。算法设计package com.bean.algorithm.basic;import java.io.BufferedReader;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.StringTokenizer;public class CountWords {public static void main(String[] args) {long startTime System.currentTimeMillis(); // 获取开始时间String string ;Map map new HashMap();try {//读取文件FileInputStream fis new FileInputStream(G://CountWords.txt);BufferedReader br new BufferedReader(new InputStreamReader(fis));String temp ;try {while ((temp br.readLine()) ! null) {string string temp;}} catch (IOException e) {// TODO: handle exceptione.printStackTrace();}} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}// 分割字符串StringTokenizer st new StringTokenizer(string); // 用于切分字符串//初始化计数器int count;//初始化word变量String word;while (st.hasMoreTokens()) {//逗号问号句号感叹号冒号双引号单引号换行符号word st.nextToken(,?.!:\\ \n);if (map.containsKey(word)) {// HashMap 保存数据count map.get(word);//计数器累加map.put(word, count 1);} else {map.put(word, 1);}}// 排序Comparator valueComparator new Comparator() {public int compare(Map.Entry o1, Map.Entry o2) {return o2.getValue() - o1.getValue();}};// 输出结果List list new ArrayList(map.entrySet());Collections.sort(list, valueComparator);System.out.println(---------------------Words分析结果 ——— 输出结果----------);for (Map.Entry entry : list) {System.out.println(entry.getKey() : entry.getValue());}long endTime System.currentTimeMillis(); // 获取结束时间System.out.println(程序运行时间 (endTime - startTime) ms);}}样例文本如下if you just want to try running findbugs against your own code, you can run findbugs using javawebstart. this will use our new gui under Java 1.5 and our old gui under java 1.4. the new gui provides a number of new features, but requires java 1.5. both use exactly the same analysis engine.程序运行结果---------------------Words分析结果 ——— 输出结果----------new:31:3gui:3use:2our:2java:25:2you:2the:2findbugs:2under:2but:1code:1against:1own:1run:1your:1running:1can:1number:1features:1same:1engine:1and:1provides:1of:1if:1just:1Java:1a:1using:1will:1old:1want:1this:1exactly:1analysis:1both:14:1javawebstart:1try:1to:1requires:1程序运行时间 6ms标签段落,util,JAVA,java,word,单词,map,new,import来源 https://blog.csdn.net/mrlin6688/article/details/100556510