Count number of words in string example java program code

import java.util.HashMap;

public class RunApplication {

    public static void main(String[] args) {

        String sentence = "this is that is not is";

        // Split sentence in word
        String[] words = sentence.split(" ");

        System.out.println("Total Word used in sentence is " + words.length);

        HashMap<String, Integer> wordCounter = new HashMap<String, Integer>();

        for (String w : words) {
            //Search for word w
            boolean foundStatus = wordCounter.containsKey(w);
            if (foundStatus) {
                //if found than get last counter value
                int lastCount = wordCounter.get(w);
                //increment with 1 and update
                wordCounter.put(w, lastCount + 1);
            } else {
                //Add new word with 1
                wordCounter.put(w, 1);
            }
        }
       
        //print all word and counter
        for (String w : wordCounter.keySet()) {
           
            System.out.println(w + " = " + wordCounter.get(w));
        }

    }
}
output
Total Word used in sentence is 6
not = 1
that = 1
is = 3
this = 1

Comments