根据单词长度排序java,统计一篇英文文件中,单词出现的次数,并按单词的长度进行排序...

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.util.Arrays;

import java.util.Comparator;

import java.util.Iterator;

import java.util.Map;

import java.util.Set;

import java.util.StringTokenizer;

import java.util.TreeMap;

/**

* 统计一个文件中单词出现的频率

* 如果首字母相同,按单词的长度输出,短的在前面

* 如果长度相等按字母顺序输出

* @author tpf

*

*/

public class WordCount {

public static void main(String[] args) {

String file = "1.txt";//英文文件(根据自己的路径设定文件地址)

//统计单词次数

Map wordTimesMap = countWordTimes(file);

//输出单词对应的次数

display(wordTimesMap);

}

/**

* 显示单词出现的次数

*

* @param wordTimesMap map

*/

private static void display(Map wordTimesMap) {

//获取到map中的key值

Set set = wordTimesMap.keySet();

//将set装换为数组

String[] obj = set.toArray(new String[0]);

//对key进行排序,匿名内部类,实现comparator接口

Arrays.sort(obj, new Comparator() {

public int compare(String o1, String o2) {

return o1.length() == 0 ?o1.compareTo(o2):(o1.length()-o2.length());

}

});

//输出单词和出现的次数

for (int i = 0; i < obj.length; i++) {

String times = wordTimesMap.get(obj[i]);

System.out.println(obj[i] + "---->" + times);

}

}

/**

* 统计给定文件中单词出现的次数

*

* @param str

*/

private static Map countWordTimes(String file) {

Map wordTimes = new TreeMap();// 存放单词出现的次数

// 读文件的流  BufferedReader in = null;  String line = "";// 保存读取的行  StringTokenizer strLine = null;  try {   in = new BufferedReader(new FileReader(file));   while ((line = in.readLine()) != null) {    strLine = new StringTokenizer(line, ",.;:()-!?/' ");// 分割单词    while (strLine.hasMoreElements()) {     String word = strLine.nextToken().toLowerCase();// 取出每个单词忽略大小写     String times = wordTimes.get(word);// 在map中获取单词出现的次数     if (times == null) {      times = "1";// 第一次出现     } else {      int n = Integer.parseInt(times);// 将次数转换int类型      ++n;// 如果单词已经存在了,就在原来的次数上加1      times = "" + n;     }     wordTimes.put(word, times);// 在放回到map中    }   }  } catch (FileNotFoundException e) {   e.printStackTrace();  } catch (IOException e) {   e.printStackTrace();  } finally {   if (in != null) {    try {     in.close();    } catch (IOException e) {     e.printStackTrace();    }    in = null;   }  }  return wordTimes; }}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值