I wish to create a int[] of count for a particular String (comprising of only lowercase English Alphabets) using Java 8 stream API. Where arr[i] denotes the count of i-th character of English dictionary (e.g. arr[0] = count of 'a' in String str while arr[2] = count of 'c' in String str. This can be simply done by:
int[] arr = new int[26];
for(char c : str.toCharArray())
arr[c-'a']++;
Or using IntSream in the 2nd way:
int[] arr = IntStream.range('a','z'+1).map(i -> (int)str.chars().filter(c -> c == i).count()).toArray();
But the problem with the second approach is that the String is traversed 26 times for each of the characters from 'a' to 'z'
Can you suggest a better way of achieving the same using java8-stream API?
PS: I know this can be done using Map but I need int[]