首页javastringJava Data Type - 如何计数“x”字母单词的数量

Java Data Type - 如何计数“x”字母单词的数量

我们想知道如何计数“x”字母单词的数量。
import java.util.HashMap;
import java.util.Map;

public class Main {
 
  public static void main(String[] args) throws Exception {
    String bubba = "this is a test this is a test";
    Map<Integer,Integer> occurrences = new HashMap<Integer,Integer>(); 

    for(String currentWord: bubba.split(" ")){
        Integer current = occurrences.get(currentWord.length());
        if(current==null){
            current = 0;
        }
        occurrences.put(currentWord.length(), current+1);
    }
    for(Integer currentKey: occurrences.keySet()){
        System.out.println("There are "+occurrences.get(currentKey)+" "+currentKey+" letter words");
    }
  }
}