Collectors is a final class that extends the Object class. It provides reduction operations, such as accumulating elements into collections, summarizing elements according to various criteria, etc.
Implementations of the Collector interface that implement various useful reduction operations, such as accumulating elements into collections, summarizing elements according to various criteria, etc.
Java 8 Collectors Class Example
Java 8 Collectors.toList() Example
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); } }
Output:
[25000.0, 30000.0, 28000.0, 28000.0, 90000.0]
Java 8 Collectors.toSet() Example
import java.util.stream.Collectors;
import java.util.Set;
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", 25000 f));
productsList.add(new Product(2, "Dell Laptop", 30000 f));
productsList.add(new Product(3, "Lenevo Laptop", 28000 f));
productsList.add(new Product(4, "Sony Laptop", 28000 f));
productsList.add(new Product(5, "Apple Laptop", 90000 f));
Set < Float > productPriceList =
productsList.stream()
.map(x - > x.price) // fetching price
.collect(Collectors.toSet()); // collecting as list
System.out.println(productPriceList);
}
}
Output:
[25000.0, 30000.0, 28000.0, 90000.0]
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);
}
}
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);
}
}
Average price is: 40200.0
Java 8 Collectors.counting() Example
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 int getId() {
return id;
}
public String getName() {
return name;
}
public float getPrice() {
return 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", 25000 f));
productsList.add(new Product(2, "Dell Laptop", 30000 f));
productsList.add(new Product(3, "Lenevo Laptop", 28000 f));
productsList.add(new Product(4, "Sony Laptop", 28000 f));
productsList.add(new Product(5, "Apple Laptop", 90000 f));
Long noOfElements = productsList.stream()
.collect(Collectors.counting());
System.out.println("Total elements : " + noOfElements);
}
}
Output:
Total elements : 5
Source code on GitHub
The source code of this post is available on GitHub Repository.
Related Java 8 Top Posts
- Java 8 Lambda Expressions
- Java 8 Functional Interfaces
- Java 8 Method References
- Java 8 Stream API
- Java 8 Optional Class
- Java 8 Collectors Class
- Java 8 StringJoiner Class
- Java 8 Static and Default Methods in Interface
- Guide to Java 8 forEach Method
- Handle NullPointerException in Controller, Service, and DAO Layer using Java 8 Optional Class
- How to Use Java 8 Stream API in Java Projects
- Migrating Source Code to Java 8
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course
Comments
Post a Comment