Java 8 Collectors.toList() Example

ToList collector can be used for collecting all Stream elements into a List instance. The important thing to remember is the fact that we can't assume any particular List implementation with this method. If you want to have more control over this, use toCollection instead.
Learn Java 8 at

Java 8 Collectors.toList() Example

import java.util.List;  
import java.util.ArrayList;  
class Product{  
    int id;  
    String name;  
    float price;  
    public Product(int id, String name, float price) {  = id;  = 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 =   
                            .map(x->x.price)         // fetching price  
                            .collect(Collectors.toList());  // collecting as list  

[25000.0, 30000.0, 28000.0, 28000.0, 90000.0]

 Java 8 Tutorials

Free Spring Boot Tutorial - 5 Hours Full Course

Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course