What is the summaryStatistics() method in Java Stream API?

The summaryStatistics method is a handy utility in the Java Stream API that returns a special class (IntSummaryStatistics, LongSummaryStatistics, or DoubleSummaryStatistics) encapsulating a set of statistical values: the count, sum, minimum, maximum, and average of numbers in the stream.

Here’s a basic example using IntSummaryStatistics:

package org.kodejava.stream;

import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;

public class IntSummaryStatisticsExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

        IntSummaryStatistics stats = numbers.stream()
                .mapToInt((x) -> x)
                .summaryStatistics();

        System.out.println("Highest number in List : " + stats.getMax());
        System.out.println("Lowest number in List  : " + stats.getMin());
        System.out.println("Sum of all numbers     : " + stats.getSum());
        System.out.println("Average of all numbers : " + stats.getAverage());
        System.out.println("Total numbers in List  : " + stats.getCount());
    }
}

Output of the code snippet:

Highest number in List : 10
Lowest number in List  : 1
Sum of all numbers     : 55
Average of all numbers : 5.5
Total numbers in List  : 10

In this example, mapToInt is used to convert the Integer stream to an IntStream, which can then use the summaryStatistics method.

The resulting IntSummaryStatistics object holds all the computed statistics, which can be accessed via its methods (getMax(), getMin(), getSum(), getAverage(), and getCount()).

It’s a quick and convenient way to get multiple useful statistics about a group of numbers. Similar functionality is provided for Long and Double streams via LongSummaryStatistics and DoubleSummaryStatistics, respectively.

The beauty of this method is that it gives you all those statistical information in one go, and you don’t need to iterate the stream multiple times to calculate different values.

How do I use averaging collectors in Java Stream API?

In Java Stream API, there are several collectors you can use to calculate the average of numbers. The purpose of averaging methods or collectors is to aggregate or combine the elements of the stream into a single summary result, which is the average of the elements.

A key feature of the Stream API is its ability to execute computation in parallel, maximizing the use of multicore architectures. In this context, the Collectors.averagingInt(), Collectors.averagingLong(), and Collectors.averagingDouble() methods provide a way to calculate the average in a thread-safe manner, taking full advantage of parallel processing if a parallel stream is used.

Using these methods, you can calculate the average of a large set of numbers without having to manually implement the average calculation or worrying about synchronization in a parallel computation environment.

Here is how you can do it:

  • Average of int numbers

To calculate the average of int numbers, you can use Collectors.averagingInt().

Assuming we have a list of integers named numbers:

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

We can calculate the average using the following code:

package org.kodejava.stream;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class AveragingInt {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

        Double average = numbers.stream()
                .collect(Collectors.averagingInt(Integer::intValue));

        System.out.println("Average value: " + average);
    }
}
  • Average of long numbers

To calculate the average of long numbers, you can use Collectors.averagingLong().

package org.kodejava.stream;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class AveragingLong {
    public static void main(String[] args) {
        List<Long> longNumbers = Arrays.asList(1L, 2L, 3L, 4L, 5L);

        Double longAverage = longNumbers.stream()
                .collect(Collectors.averagingLong(Long::longValue));

        System.out.println("Average value: " + longAverage);
    }
}
  • Average of double numbers

To calculate the average of double numbers, you can use Collectors.averagingDouble().

package org.kodejava.stream;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class AveragingDouble {
    public static void main(String[] args) {
        List<Double> doubleNumbers = Arrays.asList(1.0, 2.0, 3.0, 4.0, 5.0);

        Double doubleAverage = doubleNumbers.stream()
                .collect(Collectors.averagingDouble(Double::doubleValue));

        System.out.println("Average value: " + doubleAverage);
    }
}

The result of these averaging collectors will be a Double variable holding the average value of the numbers.

Here is another example:

package org.kodejava.stream;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ProductPriceAveraging {
    public static void main(String[] args) {
        List<Product> products = Arrays.asList(
                new Product("Product 1", 10.0),
                new Product("Product 2", 20.0),
                new Product("Product 3", 30.0));

        double averagePrice = products.stream()
                .collect(Collectors.averagingDouble(Product::getPrice));

        System.out.println("Average price: " + averagePrice);
    }

}

class Product {
    private final String name;
    private final Double price;

    public Product(String name, Double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public Double getPrice() {
        return price;
    }
}

In this example, the averagingDouble() collector is used to find the average price of all products in the list. The Product::getPrice method reference is used to tell the collector to use the getPrice() method of the Product class to retrieve the value for each product.

Overall, the averaging collectors in Java Stream API provide a concise, thread-safe, and efficient way to calculate averages over stream elements.

How do I use Collectors.joining() method?

The Collectors.joining() method is a handy utility in the java.util.stream.Collectors class that provides a Collector which concatenates input elements from a stream into a String.

Here are three versions of Collectors.joining():

  • joining(): Concatenates the input elements, separated by the empty string.
  • joining(CharSequence delimiter): Concatenates the input elements, separated by the specified delimiter.
  • joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix): Concatenates the input elements, separated by the delimiter, with the specified prefix and suffix.

Let’s see an example of each:

package org.kodejava.stream;

import java.util.stream.Collectors;
import java.util.stream.Stream;

public class CollectorsJoining {
    public static void main(String[] args) {
        // Using joining()
        String joined1 = Stream.of("Hello", "world")
                .collect(Collectors.joining());
        System.out.println(joined1);

        // Using joining(CharSequence delimiter)
        String joined2 = Stream.of("Hello", "world")
                .collect(Collectors.joining(" "));
        System.out.println(joined2);

        // Using joining(CharSequence delimiter, CharSequence prefix,
        // CharSequence suffix)
        String joined3 = Stream.of("Hello", "world")
                .collect(Collectors.joining(", ", "[", "]"));
        System.out.println(joined3);
    }
}

Output:

Helloworld
Hello world
[Hello, world]

In these examples, we use Stream.of() to create a stream of strings, and Collectors.joining() to concatenate them together, with or without delimiters, prefix, and suffix as needed.

Now, let’s consider we have a Person class and a list of Person objects. We want to join the names of all persons. Here is how to do that:

package org.kodejava.stream;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

class Person {
    private final String name;

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

public class CollectorsJoiningObjectProperty {
    public static void main(String[] args) {
        List<Person> people = Arrays.asList(
                new Person("John"),
                new Person("Rosa"),
                new Person("Doe")
        );

        String names = people.stream()
                .map(Person::getName)
                .collect(Collectors.joining(", "));

        System.out.println(names);
    }
}

Output:

John, Rosa, Doe

In the above example, we start with a List of Person objects. We create a stream from the list, then use the map() function to transform each Person into a String (their name). The collect() method is then used with Collectors.joining(), which joins all the names together into one String, separated by commas.

How do I use Collectors.toSet() method?

The Collectors.toSet() method is a method from the java.util.stream.Collectors class that provides a Collector able to transform the elements of a stream into a Set.

Here’s an example of using the Collectors.toSet() method:

package org.kodejava.stream;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class CollectorsToSet {
    public static void main(String[] args) {
        List<String> fruits = Arrays.asList("Apple", "Banana", "Orange", "Apple", "Banana", "Cherry");

        Set<String> uniqueFruits = fruits.stream()
                .collect(Collectors.toSet());

        System.out.println(uniqueFruits);
    }
}

Output:

[Apple, Cherry, Orange, Banana]

In this example, we have a List<String> of fruits, which contains some duplicate elements. When we stream the list and collect it into a Set using Collectors.toSet(), the result is a Set<String> that only includes the unique fruit names, as a Set doesn’t allow duplicate values.

Remember, like collect other terminal operation, it triggers the processing of the data and will return a collection or other specificity defined type. In case of Collectors.toSet(), the result is a Set.

How do I use Collectors.toList() method?

The Collectors.toList() method is a convenient method in the java.util.stream.Collectors class that provides a Collector to accumulate input elements into a new List.

Here is a simple example of how to use Collectors.toList():

package org.kodejava.stream;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class CollectorsToList {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);

        List<Integer> evenNumbers = numbers.stream()
                .filter(n -> n % 2 == 0)
                .collect(Collectors.toList());

        System.out.println(evenNumbers);
    }
}

Output:

[2, 4, 6, 8]

In this example, we create a stream of numbers and filter it to only include the even numbers. Then we collect the output into a List using Collectors.toList(). The result is a List<Integer> that only includes the even numbers.

Remember that collect is a terminal operation (meaning it triggers the processing of the data) and it returns a collection or other desired result type. In case of Collectors.toList(), the result is a List.