LoginSignup
1
0

More than 1 year has passed since last update.

[java] 配列の指定された文字列の出現回数を数える

Posted at

初めに

javaに限らず、ある文字列の出現回数を求めたいときがあると思う。多くのプログラミング言語では関数を使えば簡単に実装できるが、簡単に実装する。

実装

List型引数を持ち、Mapを返り値に持つgetWordCountMap関数を実装した。

public static Map<String,Integer> getWordCountMap(List<String> wordList){
        Map<String,Integer> count = new HashMap<>();
        String[] count1 = new String[wordList.size()];
        int check=0;

        for(String s:wordList){
        check = 0;
          for(String key : count.keySet()){
                if(s.equals(key)){
                    check = 1;
                }
            }
            if(check == 0){
                count.put(s,1);
            }
            else{
                count.replace(s,count.get(s)+1);
            }
         }
        return count;
    }


1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0