Word Map Counter

110 Views Asked by At

how do I instantiate a map, using the split(" ") on words, and iterate through the resulting array. * Inside of the resulting for loop, you should insert a new key to the map every time you encounter a word that is not already a key in the map, and add to the word's count value every time you encounter a word that already exists in the map.

Map<String, Integer> map = new Hash Map<String, Integer>();
    for (String s : string Array) {
        if (containerization(s)) {
            map.put(s, map.get(s) + 1);
        } else {
            map.put(s, 1);
        }
    }

  
    return map;

}

}

2

There are 2 best solutions below

2
rostIvan On

The code you may want to write accoringly to the description:

class Main {
    public static void main(String[] args) {
        String input = "Hello world Hello all";
        Map<String, Integer> wordMap = countWords(input);
        System.out.println(wordMap); // {all=1, world=1, Hello=2}
    }

    private static HashMap<String, Integer> countWords(final String text) {
        HashMap<String, Integer> wordMap = new HashMap<>();
        String[] words = text.split(" "); // split the input string by space
        for (String word : words) {
            if (wordMap.containsKey(word)) {
                wordMap.put(word, wordMap.get(word) + 1); // increment count of existing word
            } else {
                wordMap.put(word, 1); // add new word to the map
            }
        }
        return wordMap;
    }
}

Note that it can be improved this way:

class Main {
    public static void main(String[] args) {
        String input = "Hello world Hello all";
        Map<String, Integer> wordMap = countWords(input);
        System.out.println(wordMap); // {all=1, world=1, Hello=2}
    }

    private static HashMap<String, Integer> countWords(final String text) {
        HashMap<String, Integer> wordMap = new HashMap<>();
        String[] words = text.split(" "); // split the input string by space
        for (String word : words) {
            wordMap.merge(word, 1, Integer::sum);
        }
        return wordMap;
    }
}

Or that way:

class Main {
    public static void main(String[] args) {
        String input = "Hello world Hello all";
        Map<String, Integer> wordMap = countWords(input);
        System.out.println(wordMap); // {all=1, world=1, Hello=2}
    }

    private static Map<String, Integer> countWords(final String text) {
        return Arrays.stream(text.split(" "))
            .collect(groupingBy(identity(), summingInt(x -> 1)));
    }
}
0
WJS On

Here is yet another way. Use Map.merge.

Map<String, Integer> map = new HashMap<String, Integer>();
String s = "one three    five four   four"
        + "            two three five     four  four"
        + "   five five  three    two five";

\\s+ - splits on one or more white space characters

for (String word : s.split("\\s+")) {
    map.merge(word, 1, Integer::sum);
}

map.entrySet().forEach(System.out::println);

prints

four=4
one=1
two=2
five=5
three=3