Write a Java 8 Program to count no of characters in a String using Stream.Collectors

import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.Map;
import java.util.function.Function;
 
public class Myprogram {
   public static void main(String[] args) {
     Stream stream = str.chars().mapToObj(i -> (char)i).parallel();
     Map<Character, Long> counter = 
             stream.collect(Collectors.groupingBy(Function.identity(),
                            Collectors.counting()));
     System.out.println(counter.get('a'));
   }
}

Write a program to reverse a Stream?

Here we can do this in multiple ways. But without using a intermediate storage will be a better way to solve this problem:

 

Approach#1

IntStream reverse(int from, int to) {
    return IntStream.range(from, to).map(i -> to - i + from - 1);
}

reverse(0, 5).toArray();
output:
[4,3,2,1,0]