জাভা স্ট্রীম কাউন্টিং() পদ্ধতি ব্যবহার করে স্ট্রীমের উপাদানের সংখ্যা গণনা করুন। নিচে জাভা স্ট্রিম গণনা() পদ্ধতি -
বাস্তবায়নের একটি উদাহরণ দেওয়া হলউদাহরণ
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");
// count
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);
// count
long count = stream.collect(Collectors.counting());
System.out.println("Number of elements in the stream = "+count);
}
} আউটপুট
Number of elements in the stream = 6