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'));
}
}
|
Category: Stream API
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]
What do you mean by Stream?
java.util.stream package contains the majority of interfaces and classes that provide functionality to streams and aggregate operations.
A stream is a sequence of elements. Unlike a collection, it is not a data structure that stores elements. Instead, a stream carries values from a source through a pipeline.
Syntax: