• Java-使用Map集合计算文本中字符的个数


    一、题目要求

    有一个英文的文本文档a.txt , 需要读取该文本文档并计算文档中英文字符出现的次数,最后打印输出

    注意:map集合只能放入英文字符,不能够有空格数字引号等字符,所以在写代码的时候需要额外的进行判断,还需要考虑的一点是英文字符是有大小写的,但是统计在map里面就不区分大小写


    二、分析

    1、需要先获取文档的路径,并创建HashMap作为存放次数的容器,key的类型为Character存放字符,value的类型为Integer存放次数
    2、通过文档输入流读取文档中的内容,得到一串字符串
    3、遍历字符串,获得字符串中每一个字符
    4、判断字符在map集合中是否存在
    5、不存在则将其放入
    6、存在则将map集合中字符key对应的次数value取出,然后加一,再通过put方法重新放回,起到更新次数的效果
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    三、部分代码展示

    1、创建Map列表及文件路径

    FileInputStream fis = null;
    HashMap<Character, Integer> map = new HashMap<>();
    
    • 1
    • 2

    2、获得文本中的内容并返回为字符串

    while ((len = fis.read(b))!=-1){
    // 获得文件中的字符串
          s = new String(b, 0, len);
    }
    
    • 1
    • 2
    • 3
    • 4

    3、遍历字符串并获得每个字符再进行相关判断后放入map集合中

    // 遍历字符串得到每一个字符
     for (int i = 0; i < s.length() ; i++) {
         char c = s.charAt(i);
         // 判断是否是字母
         if ( ((c>='a')&&(c<='z')) || ((c>='A')&&(c<='Z'))){
         // 将字母转换为小写
         char nc = Character.toLowerCase(c);
        // 判断map集合中是否有该键
         if (map.containsKey(nc)){
         Integer num = map.get(nc);
         num++;
         map.put(nc,num);
      }else {
         map.put(nc,1);
        }
      }
    
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    4、遍历map集合得到数据

    // 遍历map集合得到数据
    Set<Character> key = map.keySet();
      for (Character c : key) {
      Integer value = map.get(c);
      System.out.println("key="+c+"----"+"value="+value);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    四、全部代码

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Set;
    
    public class totalNum {
        public static void main(String[] args) throws IOException {
            // todo 统计字母出现的次数
            FileInputStream fis = null;
            String s = null;
            HashMap<Character, Integer> map = new HashMap<>();
            try {
                fis = new FileInputStream("E:\\JavaCode\\JavaSE\\Day8-25\\src\\a1\\a.txt");
                byte[] b = new byte[1024];
                int len = 0;
    
                while ((len = fis.read(b))!=-1){
                    // 获得文件中的字符串
                     s = new String(b, 0, len);
                }
    
                // 遍历字符串得到每一个字符
                for (int i = 0; i < s.length() ; i++) {
                    char c = s.charAt(i);
                    // 判断是否是字母
                    if ( ((c>='a')&&(c<='z')) || ((c>='A')&&(c<='Z'))){
                        // 将字母转换为小写
                        char nc = Character.toLowerCase(c);
                        // 判断map集合中是否有该键
                        if (map.containsKey(nc)){
                            Integer num = map.get(nc);
                            num++;
                            map.put(nc,num);
                        }else {
                            map.put(nc,1);
                        }
                    }
    
                }
    
                // 遍历map集合得到数据
                Set<Character> key = map.keySet();
                for (Character c : key) {
                    Integer value = map.get(c);
                    System.out.println("key="+c+"----"+"value="+value);
                }
    
    
    
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                fis.close();
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    五、结果截图(部分)

    在这里插入图片描述


    六、a.txt文本

    I am happy to join with you. today in what will go down in history ,as the greatest demonstration for freedom in the history of our nation…
    Five score years ago, a great American, in whose symbolic shadow we stand today, signed the Emancipation Proclamation. This momentous decree came as a great beacon light of hope to millions of Negro slaves who had been seared in the flames of withering injustice. It came as a joyous daybreak to end the long night of bad captivity.

  • 相关阅读:
    jaotc
    popcount相关性质+从低往高的数位dp:CF1734F
    23个react常见问题
    第六章:最新版零基础学习 PYTHON 教程—Python 正则表达式(第二节 - Python 中的正则表达式与示例套装)
    SpringMVC 学习(九)之拦截器
    记一次接口分析
    HTML静态网页作业——基于html+css+javascript+jquery+bootstarp响应式成都家乡介绍网页
    简单工厂模式
    Java有根儿:Class文件以及类加载器
    Spring5框架
  • 原文地址:https://blog.csdn.net/qq_52998673/article/details/126593707