Wednesday 20 September 2017

How to use averagingInt, summingInt and summarizingInt methods of Collectors | Java 8 streams


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=ZIMzov5pfYg&list=UUhwKlOVR041tngjerWxVccw

Product.java
public class Product
{
    private String name;
    private int price;

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

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public int getPrice()
    {
        return price;
    }

    public void setPrice(int price)
    {
        this.price = price;
    }

    @Override
    public String toString()
    {
        return "Product [name=" + name + ", price=" + price + "]";
    }

}
StreamDemo.java
import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.stream.Collectors;

public class StreamDemo
{
    public static void main(String[] args)
    {
        List<Product> productList = Arrays.asList(
                new Product("potatoes", 15),
                new Product("orange", 15), new Product("lemon", 20),
                new Product("bread", 20), new Product("sugar", 30));

        /*
         * Processing the average value of all numeric elements of the
         * stream:
         * 
         * Methods averagingXX(), summingXX() and summarizingXX() can
         * work as with primitives (int, long, double) as with their
         * wrapper classes (Integer, Long, Double). One more powerful
         * feature of these methods is providing the mapping. So,
         * developer doesn’t need to use an additional map() operation
         * before the collect() method.
         */
        double averagePrice = productList.stream()
                .collect(Collectors.averagingInt(Product::getPrice));

        System.out.println("averagePrice = " + averagePrice);

        /*
         * Processing the sum of all numeric elements of the stream:
         */

        int summingPrice = productList.stream()
                .collect(Collectors.summingInt(Product::getPrice));

        System.out.println("summingPrice = " + summingPrice);

        /*
         * Collecting statistical information about stream’s elements:
         */
        IntSummaryStatistics statistics = productList.stream()
                .collect(
                        Collectors.summarizingInt(Product::getPrice));

        System.out.println("statistics = " + statistics);
        System.out.println("Average = " + statistics.getAverage());
        System.out.println("Count = " + statistics.getCount());
        System.out.println("Max = " + statistics.getMax());
        System.out.println("Min = " + statistics.getMin());
        System.out.println("Sum = " + statistics.getSum());
    }

}
Output
averagePrice = 20.0
summingPrice = 100
statistics = IntSummaryStatistics{count=5, sum=100, min=15, average=20.000000, max=30}
Average = 20.0
Count = 5
Max = 30
Min = 15
Sum = 100

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_summing_average_statistics.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/StreamDemo_summing_average_statistics

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/adf72ea7a150b1a34af5820c19f2232b522614b9/BasicJava/StreamDemo_summing_average_statistics/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment