Java 8 Collectors Class

1. Overview

Collectors is a final class that extends Object class. It provides reduction operations, such as accumulating elements into collections, summarizing elements according to various criteria, etc.
Implementations of Collector interface that implement various useful reduction operations, such as accumulating elements into collections, summarizing elements according to various criteria, etc.
Let's use discuss Collectors class API with examples.

2. Java 8 Collectors Class Example

2.1 Java Collectors Example: Fetching data as a List

import java.util.stream.Collectors;  
import java.util.List;  
import java.util.ArrayList;  
class Product{  
    int id;  
    String name;  
    float price;  
      
    public Product(int id, String name, float price) {  
        this.id = id;  
        this.name = name;  
        this.price = price;  
    }  
}  
public class CollectorsExample {  
    public static void main(String[] args) {  
        List<Product> productsList = new ArrayList<Product>();  
        //Adding Products  
        productsList.add(new Product(1,"HP Laptop",25000f));  
        productsList.add(new Product(2,"Dell Laptop",30000f));  
        productsList.add(new Product(3,"Lenevo Laptop",28000f));  
        productsList.add(new Product(4,"Sony Laptop",28000f));  
        productsList.add(new Product(5,"Apple Laptop",90000f));  
        List<Float> productPriceList =   
                productsList.stream()  
                            .map(x->x.price)         // fetching price  
                            .collect(Collectors.toList());  // collecting as list  
        System.out.println(productPriceList);  
    }  
}  

2.2 Java Collectors Example: Converting Data as a Set

import java.util.stream.Collectors;  
import java.util.Set;  
import java.util.List;  
import java.util.ArrayList;  
classProduct{  
    intid;  
    String name;  
    floatprice;  
      
    public Product(intid, String name, floatprice) {  
        this.id = id;  
        this.name = name;  
        this.price = price;  
    }  
}  
publicclass CollectorsExample {  
    publicstaticvoid main(String[] args) {  
        List<Product>productsList = new ArrayList<Product>();  
        //Adding Products  
        productsList.add(newProduct(1,"HP Laptop",25000f));  
        productsList.add(newProduct(2,"Dell Laptop",30000f));  
        productsList.add(newProduct(3,"Lenevo Laptop",28000f));  
        productsList.add(newProduct(4,"Sony Laptop",28000f));  
        productsList.add(newProduct(5,"Apple Laptop",90000f));  
        Set<Float>productPriceList =   
                productsList.stream()  
                            .map(x->x.price)         // fetching price  
                            .collect(Collectors.toSet());   // collecting as list  
        System.out.println(productPriceList);  
    }  
} 

2.3 Java Collectors Example: using sum method

import java.util.stream.Collectors;  
import java.util.List;  
import java.util.ArrayList;  
class Product{  
    int id;  
    String name;  
    float price;  
      
    public Product(int id, String name, float price) {  
        this.id = id;  
        this.name = name;  
        this.price = price;  
    }  
}  
public class CollectorsExample {  
    public static void main(String[] args) {  
        List<Product> productsList = new ArrayList<Product>();  
        //Adding Products  
        productsList.add(new Product(1,"HP Laptop",25000f));  
        productsList.add(new Product(2,"Dell Laptop",30000f));  
        productsList.add(new Product(3,"Lenevo Laptop",28000f));  
        productsList.add(new Product(4,"Sony Laptop",28000f));  
        productsList.add(new Product(5,"Apple Laptop",90000f));  
        Double sumPrices =   
                productsList.stream()  
                            .collect(Collectors.summingDouble(x->x.price));  // collecting as list  
        System.out.println("Sum of prices: "+sumPrices);  
        Integer sumId =   
                productsList.stream().collect(Collectors.summingInt(x->x.id));  
        System.out.println("Sum of id's: "+sumId);  
    }  
}

2.4 Java Collectors Example: Getting Product Average Price

import java.util.stream.Collectors;  
import java.util.List;  
import java.util.ArrayList;  
class Product{  
    int id;  
    String name;  
    float price;  
      
    public Product(int id, String name, float price) {  
        this.id = id;  
        this.name = name;  
        this.price = price;  
    }  
}  
public class CollectorsExample {  
    public static void main(String[] args) {  
        List<Product> productsList = new ArrayList<Product>();  
        //Adding Products  
        productsList.add(new Product(1,"HP Laptop",25000f));  
        productsList.add(new Product(2,"Dell Laptop",30000f));  
        productsList.add(new Product(3,"Lenevo Laptop",28000f));  
        productsList.add(new Product(4,"Sony Laptop",28000f));  
        productsList.add(new Product(5,"Apple Laptop",90000f));  
        Double average = productsList.stream()  
                         .collect(Collectors.averagingDouble(p->p.price));  
        System.out.println("Average price is: "+average);  
    }  
}  

2.5 Java Collectors Example: Counting Elements

import java.util.stream.Collectors;  
import java.util.List;  
import java.util.ArrayList;  
class Product{  
    intid;  
    String name;  
    floatprice;  
      
    public Product(intid, String name, floatprice) {  
        this.id = id;  
        this.name = name;  
        this.price = price;  
    }  
    publicint getId() {  
        returnid;  
    }  
    public String getName() {  
        returnname;  
    }  
    publicfloat getPrice() {  
        returnprice;  
    }  
}  
publicclass CollectorsExample {  
    publicstaticvoid main(String[] args) {  
        List<Product>productsList = new ArrayList<Product>();  
        //Adding Products  
        productsList.add(new Product(1,"HP Laptop",25000f));  
        productsList.add(new Product(2,"Dell Laptop",30000f));  
        productsList.add(new Product(3,"Lenevo Laptop",28000f));  
        productsList.add(new Product(4,"Sony Laptop",28000f));  
        productsList.add(new Product(5,"Apple Laptop",90000f));  
        Long noOfElements = productsList.stream()  
                               .collect(Collectors.counting());  
        System.out.println("Total elements : "+noOfElements);  
    }  
} 
The source code of this post is available on GitHub Repository.

4. Related Java 8 Top Posts

Comments