Sample output All Is Well : { =2, A=1, s=1, e=1, W=1, I=1, l=4}
Asked
Active
Viewed 1,812 times
0
-
Can you share what you tried? – Eran Jan 24 '22 at 13:42
-
I know how to do it in normal way like using map but using lambda or streams (java-8) I don't have idea – JAVA LOVER Jan 24 '22 at 14:26
-
Duplicate of https://stackoverflow.com/q/70224399/160256 – Keegan Jan 25 '22 at 02:33
2 Answers
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