Java 用hashmap统计词频
来源:互联网 发布:java关键字 false 编辑:程序博客网 时间:2024/05/21 17:02
C:\\Temp\\1\\a.txt 内容:
1,a,282,b,353,c,284,d,355,e,286,a,287,b,358,c,289,a,28
public class FileTest{ static File filea = new File("C:\\Temp\\1\\a.txt"); static HashMap<String,Integer> hashmap = new HashMap<String,Integer>(); public static void main(String[] args) throws IOException { BufferedReader bra = new BufferedReader(new FileReader(filea)); Scanner sa = new Scanner(bra); while(sa.hasNextLine()){ String line = sa.nextLine(); String name = line.split(",")[1]; if (hashmap.containsKey(name)) hashmap.put(name, hashmap.get(name) + 1); else hashmap.put(name, 1); } for (Entry<String, Integer> entry : hashmap.entrySet()){ Object key = entry.getKey(); Object val = entry.getValue(); System.out.println(key.toString() + " " + val.toString()); } }}
打印
d 1
e 1
b 2
c 2
a 3
注意遍历hashmap的遍历:
a. HashMap的循环,如果既需要key也需要value,直接用
b. 如果只是遍历key而无需value的话,可以直接用
0 0
- Java 用hashmap统计词频
- HashMap统计词频的方法
- 用java写的词频统计
- java 英文词频统计
- java词频统计
- Java 词频统计
- 【java】以词频升序统计文本词频
- 使用JAVA进行词频统计
- java实现词频统计程序
- 用 bash 脚本统计词频
- 词频统计
- 词频统计
- 词频统计
- 词频统计
- 词频统计
- 词频统计
- 词频统计
- 词频统计
- QT中的内存管理一个小经验
- C#学习日记06---数据类型 之 浮点数类型
- hdu 5486 Difference of Clustering
- HTML5 Form Data 对象的使用-jQuery
- UIScrollView与NSTimer计时器、分页指示器控件的使用
- Java 用hashmap统计词频
- 使用多态的注意事项
- 定位服务 CLLocationManager 的简单用法
- 网站推荐大全
- 随笔分类 - 无废话ExtJs系列教程
- JAVA注解
- ZOJ-3826-Hierarchical Notation【哈希】
- linux 技巧:使用 screen 管理你的远程会话http://www.ibm.com/developerworks/cn/linux/l-cn-screen/
- ListView与ViewPager组合(一)