জাভা স্ট্রিমের counting() মেথড ব্যবহার করে স্ট্রিমে এলিমেন্টের সংখ্যা গণনা করা যায়। জাভা স্ট্রিম counting() মেথড ইম্প্লিমেন্ট করার একটি উদাহরণ −
উদাহরণ
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Demo {
public static void main(String[] args) {
Stream<String> stream = Stream.of("Kevin", "Jofra","Tom", "Chris", "Liam");
// কাউন্ট
long count = stream.collect(Collectors.counting());
System.out.println("Number of elements in the stream = "+count);
}
}
আউটপুট
Number of elements in the stream = 5
আরেকটি উদাহরণ যেখানে আমাদের ইন্টিজার এলিমেন্টের স্ট্রিম আছে −
উদাহরণ
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Demo {
public static void main(String[] args) {
Stream<Integer> stream = Stream.of(5, 10, 20, 40, 80, 160);
// কাউন্ট
long count = stream.collect(Collectors.counting());
System.out.println("Number of elements in the stream = "+count);
}
}
আউটপুট
Number of elements in the stream = 6