首页 > 编程语言 > 详细

Java 用hashmap统计词频

时间:2021-08-09 14:05:50      阅读:14      评论:0      收藏:0      [点我收藏+]
1,a,28
2,b,35
3,c,28
4,d,35
5,e,28
6,a,28
7,b,35
8,c,28
9,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

Java 用hashmap统计词频

原文:https://www.cnblogs.com/luweiweicode/p/15117325.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!