Thursday, March 21, 2024

Java Stream - count() With Examples

In this tutorial you’ll learn about the count() method in the Java Stream API with the help of few examples.

count() method in Java

count() method returns the count of elements in the stream.

Syntax of count() method-

long count()

Some important points about count() method-

  1. It is a special case of a reduction operation which means it takes a sequence of input elements and reduce them into a single summary result.
  2. It is a terminal operation which means after count() operation the stream pipeline is considered consumed, and can no longer be used.

count() method Java examples

1. In the first example count() is used to get the count of elements in a List where List is used as a stream source.

public class StreamCountDemo {

  public static void main(String[] args) {
    List<Integer> numList = Arrays.asList(7, 5, 18, -11, 22, -8);
    long elementCount = numList.stream().count();
    System.out.println("Count of elements- " + elementCount);  
  }
}

Output

Count of elements- 6

2. Since count is a terminal operation so stream is considered closed after count operation but before count, intermediate operations like filter can be used to filter out certain elements and then get the count of the resulting stream. For example if we want count of positive elements in a list.

public class StreamCountDemo {

  public static void main(String[] args) {
    List<Integer> numList = Arrays.asList(7, 5, 18, -11, 22, -8);
    long elementCount = numList.stream().filter(n -> n > 0).count();
    System.out.println("Count of elements- " + elementCount);  
  }
}

Output

Count of elements- 4

3. In the following example count is used to get the count of employees having salary greater than 10000.

Employee class

public class Employee {
  private String empId;
  private int age;
  private String name;
  private char gender;
  private int salary;
  Employee(String empId, int age, String name, char gender, int salary){
    this.empId = empId;
    this.age = age;
    this.name = name;
    this.gender = gender;
    this.salary = salary;
  }
  public String getEmpId() {
    return empId;
  }

  public int getAge() {
    return age;
  }

  public String getName() {
    return name;
  }

  public char getGender() {
    return gender;
  }

  public int getSalary() {
    return salary;
  }
  @Override
  public String toString() {
      return "Emp Id: " +  getEmpId() + " Name: " + getName() + " Age: " + getAge();
  }
}
public class StreamCountDemo {

  public static void main(String[] args) {
    List<Employee> empList = Arrays.asList(new Employee("E001", 40, "Ram", 'M', 5000), 
                new Employee("E002", 35, "Shelly", 'F', 7000), 
                new Employee("E003", 40, "Remington", 'M', 5000), 
                new Employee("E004", 37, "Bianca", 'F', 11000),
                new Employee("E005", 35, "Dominic", 'M', 7000), 
                new Employee("E006", 28, "Amanda", 'F', 14000));
    long elementCount = empList.stream().filter(e -> e.getSalary() > 10000).count();
    System.out.println("Count of elements- " + elementCount);  
  }
}

Output

Count of elements- 2

That's all for this topic Java Stream - count() With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Advanced Tutorial Page


Related Topics

  1. Java Stream - boxed() With Examples
  2. Java Stream - sorted() With Examples
  3. Java Stream - distinct() With Examples
  4. Parallel Stream in Java Stream API
  5. Java Stream API Interview Questions And Answers

You may also like-

  1. Type Erasure in Java Generics
  2. SerialVersionUID And Versioning in Java Serialization
  3. Difference Between CountDownLatch And CyclicBarrier in Java
  4. Difference Between StackOverflowError and OutOfMemoryError in Java
  5. Docker Tutorial: Introduction to Docker
  6. Benefits, Disadvantages And Limitations of Autowiring in Spring
  7. Spring MVC JSON as Response Example
  8. Angular Access Control CanActivate Route Guard Example

No comments:

Post a Comment