统计字符串字母个数的几种方法 Java

1.统计字符串字母个数(并且保持字母顺序)

比如: aabbbbbbbba喔喔bcab  cdabc  deaaa

目前我做知道的有5种方式,如果你还有更好的,欢迎赐教

要求:统计字符串的字符个数,最好按顺序输出每个字符的个数

[html]  view plain  copy
  1.   
[html]  view plain  copy
  1. //方式1  
  2.     public static void letterCount1(String s) {  
  3.         s=s.replaceAll(" +", "");  
  4.          //1,转换成字符数组  
  5.         char c[]=s.toCharArray();  
  6.          
  7.         Map<Character, Integer> tree=new TreeMap<Character, Integer>();  
  8.         for (int i = 0; i < c.length; i++) {  
  9.         //第一次:a,1  
  10.         //第二次:a,2    
  11.          //2,获取键所对应的值  
  12.         Integer value=tree.get(c[i]);  
  13.          //3,存储判断  
  14.         tree.put(c[i], value==null? 1:value+1);  
  15.         }  
  16.         System.out.println(tree);  
  17.       
  18.     }  
  19.        
  20.     //方式2  使用流  
  21.     //这个在测试特殊字符,比如\    \n时,他的顺序会不对,这个是Map造成的  
  22.     //解决办法使用TreeMap  
  23.     public static void letterCount2(String s) {  
  24.         s=s.replaceAll(" +", "");  
  25.         TreeMap<String, Long> result = Arrays.stream(s.split(""))  
  26.                                 .sorted()  
  27. //                              .collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));  
  28.                                 .collect(Collectors.groupingBy(Function.identity(),TreeMap::new,Collectors.counting()));  
  29.         System.out.println(result);  
  30.           
  31.     }  
  32.     //或者  
  33.     public static void letterCount2_1(String s) throws Exception {  
  34.         s=s.replaceAll(" +", "");  
  35.         Stream<String> words = Arrays.stream(s.split(""));  
  36.         Map<String, Integer> wordsCount = words.collect(Collectors.toMap(k -> k, v -> 1,  
  37.                                                               (i, j) -> i + j));  
  38.         System.out.println(wordsCount);  
  39.     }  
  40.       
  41.     //方式3 使用Collections.frequency  
  42.     //其实就是字符串变成集合存每个字串,把每个字串循环跟集合比较  
  43.     public static void letterCount3(String s) {  
  44.         s=s.replaceAll(" +", "");  
  45.         List<String> list=Arrays.asList(s.split(""));  
  46.         Map<String,Integer> map=new TreeMap<String, Integer>();  
  47.         for (String str : list) {  
  48.             map.put(str, Collections.frequency(list, str));  
  49.         }  
  50.         System.out.println(map);  
  51.     }  
  52.       
  53.     //方式4  
  54.     public static void letterCount4(String s) {  
  55.         s=s.replaceAll(" +", "");  
  56.         String[] strs = s.split("");  
  57.         Map<String,Integer> map=new TreeMap<String, Integer>();  
  58.         for (String str : strs) {  
  59.             map.put(str, stringCount(s, str));  
  60.         }  
  61.         System.out.println(map);  
  62.     }  
  63.       
  64.       
  65.     //方式5  
  66.     public static void letterCount5(String s) {  
  67.         s=s.replaceAll(" +", "");  
  68.         String[] strs = s.split("");  
  69.         Map<String,Integer> map=new TreeMap<String, Integer>();  
  70.         for (String str : strs) {  
  71.             map.put(str, stringCount2(s, str));  
  72.         }  
  73.         System.out.println(map);  
  74.     }  
  75.       
  76.       
  77.       
  78.     //巧用split  
  79.     public static int stringCount(String maxstr, String substr) {  
  80.         // 注意  
  81.         // 1.比如qqqq,没有找到,则直接返回这个字符串  
  82.         // 2.比如qqqjava,末尾没有其他字符,这时也不会分割,所以可以添加一个空格  
  83.         // 3.java11开头没有字符,没有关系,自动空填充  
  84.         // 4.对于特殊字符,要注意使用转义符  
  85.         int count = (maxstr + " ").split(substr).length - 1;  
  86.         // System.out.println("\"" + minstr + "\"" + "字符串出现次数:" + count);  
  87.         return count;  
  88.     }  
  89.   
  90.     //如果要不区分大小写,则compile(minstr,CASE_INSENSITIVE)  
  91.     public static int stringCount2(String maxstr, String substr) {  
  92.         int count = 0;  
  93.         Matcher m = Pattern.compile(substr).matcher(maxstr);  
  94.         while (m.find()) {  
  95.             count++;  
  96.         }  
  97.         return count;  
  98.     }  

2.统计字符串的单词个数

这个其实跟上面一样的,下面只写一个简洁的方法

[html]  view plain  copy
  1. public static void wordStringCount(String s) {  
  2.     //这里开始是字符串,分割后变成字符串流  
  3.        Map<String, Long> result = Arrays.stream(s.split("\\s+"))  
  4.                                       .map(word -> word.replaceAll("[^a-zA-Z]", ""))  
  5.                                               .collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));  
  6.        System.out.println(result);  
  7.       
  8.    }  

3.统计文本单词个数

[html]  view plain  copy
  1. //统计一个文本中单词的个数  
  2.    public static void wordFileCount(String path) throws IOException{  
  3.     //这里一开始字符串流  
  4.     //先分割  
  5.     //在变成字符流  
  6.     //在筛选  
  7.      Map<String, Long> result = Files.lines(Paths.get(path),Charset.defaultCharset())  
  8.                              .parallel()  
  9.                   //字符串流--分割--字符串流  
  10.                  .flatMap(str->Arrays.stream(str.split(" +")))   
  11.                  .map(word -> word.replaceAll("[^a-zA-Z]", ""))  
  12.                 //去掉空  
  13.                  .filter(word->word.length()>0)   
  14.              .collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));  
  15.     System.out.println(result);  
  16.    }  
[html]  view plain  copy
  1. //优化:更精确的是根据非单词来分组  
  2.    public static void wordFileCount0(String path) throws IOException{  
  3.     Map<String, Long> result = Files.lines(Paths.get(path),Charset.defaultCharset())  
  4.             .parallel()  
  5.             //字符串流--分割--字符串流  
  6.             .flatMap(str->Arrays.stream(str.split("[^a-zA-Z]+")))   
  7.             //去掉\n  
  8.             .filter(word->word.length()>0)   
  9.             .collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));  
  10.     System.out.println(result);  
  11.    }  
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值