This method prints the count of each character in a String using Stream functions, preserving first-occurrence order via a LinkedHashMap.
Code Example:
public static void findCountOfChars(String s) {
Map<String, Long> map = Arrays.stream(s.split(""))
.map(String::toLowerCase)
.collect(Collectors.groupingBy(
str -> str,
LinkedHashMap::new,
Collectors.counting()));
System.out.println(map);
}Input:
String s = "string data to count each character";
Output:
{s=1, t=5, r=3, i=1, n=2, g=1, =5, d=1, a=5, o=2, c=4, u=1, e=2, h=2}