Java8: Create HashMap with character count of a String(Java8:使用字符串的字符数创建 HashMap)
问题描述
想知道有没有比如下计算给定字符串的字符数更简单的方法?
Wondering is there more simple way than computing the character count of a given string as below?
String word = "AAABBB";
Map<String, Integer> charCount = new HashMap();
for(String charr: word.split("")){
Integer added = charCount.putIfAbsent(charr, 1);
if(added != null)
charCount.computeIfPresent(charr,(k,v) -> v+1);
}
System.out.println(charCount);
推荐答案
计算字符串中每个字符出现次数的最简单方法,完全支持 Unicode (Java 11+)1:
Simplest way to count occurrence of each character in a string, with full Unicode support (Java 11+)1:
String word = "AAABBB";
Map<String, Long> charCount = word.codePoints().mapToObj(Character::toString)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println(charCount);
1) 完全支持 Unicode 的 Java 8 版本在答案的末尾.
输出
{A=3, B=3}
<小时>
更新:对于 Java 8+(不支持来自补充平面的字符,例如表情符号):
UPDATE: For Java 8+ (doesn't support characters from supplemental planes, e.g. emoji):
Map<String, Long> charCount = IntStream.range(0, word.length())
.mapToObj(i -> word.substring(i, i + 1))
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
<小时>
更新 2: 也适用于 Java 8+.
UPDATE 2: Also for Java 8+.
我错了,以为 codePoints()
直到 Java 9 才添加.它是在 Java 8 中添加到 CharSequence
接口,因此 String
在 Java 8 中,并显示为 在 Java 9 中添加 用于更高版本的 javadoc.
I was mistaken, thinking that codePoints()
wasn't added until Java 9. It was added in Java 8 to the CharSequence
interface, so it doesn't show in javadoc for String
in Java 8, and shows as added in Java 9 for later versions of the javadoc.
但是,Character.toString (int codePoint)
方法直到 Java 11 才被添加,所以要使用 Character.toString (char c)
方法,我们可以使用 chars()
在 Java 8 中:
However, the Character.toString(int codePoint)
method wasn't added until Java 11, so to use the Character.toString(char c)
method, we can use chars()
in Java 8:
Map<String, Long> charCount = word.chars().mapToObj(c -> Character.toString((char) c))
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
或者对于完整的 Unicode 支持,包括.补充平面,我们可以使用 codePoints()
和 String(int[] codePoints, int offset, int count)
构造函数,在 Java 8 中:
Or for full Unicode support, incl. supplemental planes, we can use codePoints()
and the String(int[] codePoints, int offset, int count)
constructor, in Java 8:
Map<String, Long> charCount = word.codePoints()
.mapToObj(cp -> new String(new int[] { cp }, 0, 1))
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
这篇关于Java8:使用字符串的字符数创建 HashMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java8:使用字符串的字符数创建 HashMap
基础教程推荐
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 降序排序:Java Map 2022-01-01