Home

10. Using Streams

Home » Java Quick Start » Quick Start Tutorial » 10. Using Streams

Tips!

Streams were introduced as a new feature in Java 8. These allow you to perform a series of steps or actions on an object.

Let's extend the ReadTextFile example with some extra functions...

First, we need to add extra imports:

import java.util.IntSummaryStatistics;
import java.util.stream.Collectors;

And we add some output in the main method:

public static void main (String[] args) {
    List persons = loadPersons();
    System.out.println("Number of persons loaded from CSV file: " + persons.size());
    for (Person person : persons) {
        System.out.println(person.getFullName() + ", age: " + person.getAge());
    }
    System.out.println("------------------------------------------------");
    System.out.println("Number of persons with first name");
    System.out.println("    * 'Matthew':     " 
        + countFirstName(persons, "Matthew"));
    System.out.println("    * 'Charlotte':   " 
        + countFirstName(persons, "Charlotte"));
    System.out.println("------------------------------------------------");
    IntSummaryStatistics stats = getAgeStats(persons);
    System.out.println("Minimum age: " + stats.getMin());
    System.out.println("Maximum age: " + stats.getMax());
    System.out.println("Average age: " + stats.getAverage());
}

Of course, we need to add the methods "countFirstName" and "getAgeStats" which are used in this extra code in the "main"-method:

public static int countFirstName(List persons, String firstName) {
    return (int) persons.stream()
        .filter(p -> p.getFirstName().equals(firstName))
        .count();
}
public static IntSummaryStatistics getAgeStats(List persons) {
    return persons.stream()
        .mapToInt(Person::getAge)
        .summaryStatistics();
}

As you see, we start by converting the list into a stream, by adding ".stream()" behind the "persons"-list. We can further handle that stream step-by-step to obtain certain results.

  • countFirstName
    • filter is used to get all the persons with the given first name
    • collect is used to make a list of all the matching persons
    • size returns the number of items in the collected list
  • getAgeStats
    • mapToInt is used to only use the ages from all the persons
    • summaryStatistics returns an IntSummaryStatistics-object from the list from which we can get different values

When we run this same application again now, we get this output:

$ java ReadTextFile.java
Number of persons loaded from CSV file: 100
Ada Gomez, age: 40
Bernard Jordan, age: 28
Mittie Vaughn, age: 64
Miguel Clarke, age: 39
...
Alexander McCoy, age: 46
Callie Fitzgerald, age: 32
------------------------------------------------
Number of persons with first name
    * 'Matthew':     2
    * 'Charlotte':   4
------------------------------------------------
Minimum age: 18
Maximum age: 65
Average age: 42.78

Subscribe to foojay updates:

https://foojay.io/feed/
Copied to the clipboard