0

Sample output All Is Well : { =2, A=1, s=1, e=1, W=1, I=1, l=4}

2 Answers2

0

You may try this,

    Arrays.stream("inputstring".split(""))
            .map(String::toLowerCase)
            .collect(Collectors.groupingBy(s -> s, LinkedHashMap::new, Collectors.counting()))
            .forEach((k, v) -> System.out.println(k + " = " + v));
VenkatN
  • 11
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 04 '22 at 22:58
0

Unordered:

"All Is Well"
    .chars()
    .mapToObj(c -> (char) c)
    .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
    .forEach((k, v) -> System.out.println("'" + k + "' = " + v));

Alphabetically ordered & lowercased:

"All Is Well"
    .chars()
    .mapToObj(c -> (char) c)
    .map(Character::toLowerCase)
    .collect(Collectors.groupingBy(s -> s, TreeMap::new, Collectors.counting()))
    .forEach((k, v) -> System.out.println("'" + k + "' = " + v));

Sample output:

' ' = 2
'a' = 1
'e' = 1
'i' = 1
'l' = 4
's' = 1
'w' = 1
Fuad Efendi
  • 155
  • 1
  • 9