单词的出现次数
来源:互联网 发布:淘宝苏哥战地1购买流程 编辑:程序博客网 时间:2024/04/28 08:30
统计一个文本中单词出现的次数,然后按照单词的字典序显示这些单词以及他们对应的次数
import java.util.*;public class CountOccurrenceOfWords { public static void main(String[] args) { // Set text in a string String text = "Good morning. Have a good class. " + "Have a good visit. Have fun!"; // Create a TreeMap to hold words as key and count as value TreeMap<String, Integer> map = new TreeMap<String, Integer>(); String[] words = text.split("[ \n\t\r.,;:!?(){}]"); for (int i = 0; i < words.length; i++) { String key = words[i].toLowerCase(); if (words[i].length() > 0) { if (map.get(key) == null) { map.put(key, 1); } else { int value = map.get(key).intValue(); value++; map.put(key, value); } } } // Get all entries into a set Set<Map.Entry<String, Integer>> entrySet = map.entrySet(); // Get key and value from each entry for (Map.Entry<String, Integer> entry: entrySet) System.out.println(entry.getValue() + "\t" + entry.getKey()); }}
运用了treemap的知识。
知识点复习:
1)
HashMap中条目的顺序是随机的,而TreeMap中的条目是按照键值的升序排列的,LinkedHashMap中的条目则是按照元素最后一次被访问的时间从早到晚排序的
0 0
- 单词的出现次数
- 统计单词出现的次数
- baidu单词出现的次数
- 统计单词出现的次数
- 统计单词出现的次数
- 统计单词出现的次数。
- 统计单词出现的次数并按单词出现的次数顺序输出单词及其次数
- 统计文章中单词出现的次数
- 统计连续出现次数最多的单词
- 统计所有'单词'出现的次数
- 找出出现次数前十的单词
- 统计某个单词出现的次数
- 统计文章中单词出现的次数
- JS统计单词出现的次数。
- 用Java统计单词出现的次数
- 统计单词出现次数
- 记录连续重复出现的单词以及出现的次数
- 字符串中出现的单词及出现次数(map)
- Ruby on Rails初涉——安装
- 百度2014研发类校园招聘笔试题解答
- 使用CloudWatch Logs 收集应用程序日志
- cobertura-maven-plugin用法
- HDu 1198 Farm Irrigation
- 单词的出现次数
- Jquery 菜单插件之 Superfish jQuery菜单
- day26
- Struts2、SpringMVC、Servlet(Jsp)性能对比测试
- 视图导航栏UInavigationController
- 动机与结果
- SQL列字符串拼接
- 黑马程序员-Foundation1-NSString 的 compare方法
- ios中正确的裁剪图片,这样不必要每次都向服务器下载图片。