Stream method count() with different data type in Java
A stream method count() used to determine a number of elements in this stream. It returns long value that represents a count of elements in the stream.
Syntax
 1 long count()
return : It returns number of elements present in the stream object.
count() with number
 1 package com.java;
 2 
 3 import java.util.Arrays;
 4 import java.util.List;
 5 
 6 public class Main {
 7 
 8     public static void main(String[] args) {
 9 
 10         List<Integer> list = Arrays.asList(12, 30, 4, 55, 3, 10);
 11         System.out.println("Original list : "+ list);
 12 
 13         long res = list.stream().count();
 14         System.out.println("An elements in string is : "+ res);
 15     }
 16 }
In the above example, a list of number is declared and stream using collection method that returns stream object. A stream method count() is called that returns number of elements in the stream. The result is assigned to the variable and print.
Output
 1 Original list : [12, 30, 4, 55, 3, 10]
 2 An elements in string is : 6

count() with string

count() with string
 1 package com.java;
 2 
 3 import java.util.Arrays;
 4 import java.util.List;
 5 
 6 public class StringExp {
 7 
 8     public static void main(String[] args) {
 9 
 10         List<String> elements = Arrays.asList("Hi", "Hello", "world",
 11                 "in", "Java");
 12         // print original list
 13         System.out.println("An original list elements : " + elements);
 14 
 15         long res = elements.stream().count();
 16         System.out.println("An elements in stream is : " + res);
 17     }
 18 }
In the above example, a list of stream is declared and stream using collection method stream() that returns stream object. A stream object count called that returns long value that specifies an elements in the stream.
Output
 1 An original list elements : [Hi, Hello, world, in, Java]
 2 An elements in stream is : 5

count() with Custom object with filter

count() with Custom object with filter
 1 package com.java;
 2 
 3 import java.util.Arrays;
 4 import java.util.List;
 5 
 6 public class OrderExp {
 7     public static void main(String[] args) {
 8         Product pd1 = new Product(100, "Phone", 10, 120000.32);
 9         Product pd2 = new Product(101, "TV", 7, 48730.51);
 10         Product pd3 = new Product(102, "Laptop", 10, 439482.05);
 11         Product pd4 = new Product(103, "Watch", 7, 10000.44);
 12 
 13         List<Product> products = Arrays.asList(pd1, pd2, pd3, pd4);
 14 
 15         long res = products.stream()
 16                         .filter(prd -> prd.getQuantity() > 8)
 17                                 .count();
 18 
 19         System.out.println("Product with more than 8 quantity is : " + res);
 20     }
 21 }
In the above example, a list of product is created and stream using collection method stream that return stream object. A stream method filter() called that filter elements whose quantity is less than equal to 8 and return stream. A resulting stream elements count returned by the count() method and print.
Output
 1 Product with more than 8 quantity is : 2
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us