Java8:使用字符串的字符数创建 HashMap

时间:2022-11-16
本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:在 Java Map 中查找重复值? 下一篇:创建 HashMap 的不同方法

相关文章

最新文章