1.統計字符串字母個數(并且保持字母順序)
比如: aabbbbbbbba喔喔bcab cdabc deaaa
目前我做知道的有5種方式噢,如果你還有更好的,歡迎賜教
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
//方式1 public 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]); // 反編譯:Integer value = (Integer)tree.get(Character.valueOf(c[i])); //3,存儲判斷 tree.put(c[i], value==null? 1:value+1); } //如果要求結果格式:a(5)b(4)c(3)d(2)e(1) StringBuilder sbu = new StringBuilder(); for(Character key:tree.keySet()){ Integer count = tree .get(key); sbu.append(key).append("(").append(count).append(")"); } //將sbu轉換為字符串 System.out.println(sbu.toString()); } //方式2 使用流 //這個在測試特殊字符,比如\ \n時,他的順序會不對,這個是Map造成的 //解決辦法使用TreeMap public 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); } //方式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); } //方式4 public 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); } //方式5 public 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); } //巧用split public 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.統計字符串的單詞個數(只限英文)
這個其實跟上面一樣的,下面只寫一個簡潔的方法
1
2
3
4
5
6
7
8
|
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.統計文本單詞個數(只限英文)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
//統計一個文本中單詞的個數 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); } |
4.其他不相干的
我們知道,可變參數列表,可以不傳參數的
對于
1
2
3
4
5
6
7
8
|
public void testName() { System.out.println("a"); } public void testName(String ... s) { //不傳參數,s會默認初始化一個對象 System.out.println("b"); } |
此時調用testName() 打印什么呢?,會打印a,會自動匹配參數真正為空的方法
以上這篇java8 統計字符串字母個數的幾種方法總結(推薦)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/u011165335/article/details/76154510