java8 统计字符串字母个数的几种方法总结(推荐)

2025-05-29 0 62

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 统计字符串字母个数的几种方法总结(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。

原文链接:http://blog.csdn.net/u011165335/article/details/76154510

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 java8 统计字符串字母个数的几种方法总结(推荐) https://www.kuaiidc.com/113679.html

相关文章

发表评论
暂无评论