本文主要是介绍java8 统计字符串字母个数的几种方法(有你没见到过的),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.统计字符串字母个数(并且保持字母顺序)
比如: aabbbbbbbba喔喔bcab cdabc deaaa
目前我做知道的有5种方式,如果你还有更好的,欢迎赐教
要求:统计字符串的字符个数,最好按顺序输出每个字符的个数
//方式1public static void letterCount1(String s) {s=s.replaceAll(" +", "");//1,转换成字符数组char c[]=s.toCharArray();Map<Character, Integer> tree=new TreeMap<Character, Integer>();for (int i = 0; i < c.length; i++) {//第一次:a,1//第二次:a,2 //2,获取键所对应的值Integer value=tree.get(c[i]);//3,存储判断tree.put(c[i], value==null? 1:value+1);}System.out.println(tree);}//方式2 使用流//这个在测试特殊字符,比如\ \n时,他的顺序会不对,这个是Map造成的//解决办法使用TreeMappublic static void letterCount2(String s) {s=s.replaceAll(" +", "");TreeMap<String, Long> result = Arrays.stream(s.split("")).sorted()
// .collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));.collect(Collectors.groupingBy(Function.identity(),TreeMap::new,Collectors.counting()));System.out.println(result);}//或者public static void letterCount2_1(String s) throws Exception {s=s.replaceAll(" +", "");Stream<String> words = Arrays.stream(s.split(""));Map<String, Integer> wordsCount = words.collect(Collectors.toMap(k -> k, v -> 1,(i, j) -> i + j));System.out.println(wordsCount);}//方式3 使用Collections.frequency//其实就是字符串变成集合存每个字串,把每个字串循环跟集合比较public static void letterCount3(String s) {s=s.replaceAll(" +", "");List<String> list=Arrays.asList(s.split(""));Map<String,Integer> map=new TreeMap<String, Integer>();for (String str : list) {map.put(str, Collections.frequency(list, str));}System.out.println(map);}//方式4public static void letterCount4(String s) {s=s.replaceAll(" +", "");String[] strs = s.split("");Map<String,Integer> map=new TreeMap<String, Integer>();for (String str : strs) {map.put(str, stringCount(s, str));}System.out.println(map);}//方式5public static void letterCount5(String s) {s=s.replaceAll(" +", "");String[] strs = s.split("");Map<String,Integer> map=new TreeMap<String, Integer>();for (String str : strs) {map.put(str, stringCount2(s, str));}System.out.println(map);}//巧用splitpublic static int stringCount(String maxstr, String substr) {// 注意// 1.比如qqqq,没有找到,则直接返回这个字符串// 2.比如qqqjava,末尾没有其他字符,这时也不会分割,所以可以添加一个空格// 3.java11开头没有字符,没有关系,自动空填充// 4.对于特殊字符,要注意使用转义符int count = (maxstr + " ").split(substr).length - 1;// System.out.println("\"" + minstr + "\"" + "字符串出现次数:" + count);return count;}//如果要不区分大小写,则compile(minstr,CASE_INSENSITIVE)public static int stringCount2(String maxstr, String substr) {int count = 0;Matcher m = Pattern.compile(substr).matcher(maxstr);while (m.find()) {count++;}return count;}
2.统计字符串的单词个数
这个其实跟上面一样的,下面只写一个简洁的方法
public static void wordStringCount(String s) {//这里开始是字符串,分割后变成字符串流Map<String, Long> result = Arrays.stream(s.split("\\s+")).map(word -> word.replaceAll("[^a-zA-Z]", "")).collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));System.out.println(result);}
3.统计文本单词个数
//统计一个文本中单词的个数public static void wordFileCount(String path) throws IOException{//这里一开始字符串流//先分割//在变成字符流//在筛选Map<String, Long> result = Files.lines(Paths.get(path),Charset.defaultCharset()).parallel()//字符串流--分割--字符串流.flatMap(str->Arrays.stream(str.split(" +"))) .map(word -> word.replaceAll("[^a-zA-Z]", ""))//去掉空.filter(word->word.length()>0) .collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));System.out.println(result);}
//优化:更精确的是根据非单词来分组public static void wordFileCount0(String path) throws IOException{Map<String, Long> result = Files.lines(Paths.get(path),Charset.defaultCharset()).parallel()//字符串流--分割--字符串流.flatMap(str->Arrays.stream(str.split("[^a-zA-Z]+"))) //去掉\n.filter(word->word.length()>0) .collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));System.out.println(result);}
这篇关于java8 统计字符串字母个数的几种方法(有你没见到过的)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!