Wednesday, 20 September 2017

How to use groupingBy , partitioningBy methods of Collectors | Java 8 streams | Streams in Java 8


Click here to watch in Youtube :
https://www.youtube.com/watch?v=R57Nv_yD9xU&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.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
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));

        /*
         * Grouping of stream’s elements according to the specified
         * function:
         * 
         * The stream was reduced to the Map which groups all products
         * by their price.
         */
        Map<Integer, List<Product>> map = productList.stream()
                .collect(Collectors.groupingBy(Product::getPrice));
        
        Set<Entry<Integer, List<Product>>> entrySet = map.entrySet();
        for (Entry<Integer, List<Product>> entry : entrySet)
        {
            System.out.println(entry.getKey() +" = "+entry.getValue());
        }
        
        System.out.println("---------------------------------------------------");

        /*
         * Dividing stream’s elements into groups according to some
         * predicate:
         */
        Map<Boolean, List<Product>> partionedMap = productList
                .stream().collect(Collectors.partitioningBy(
                        element -> element.getPrice() > 25));
        
        
        Set<Entry<Boolean, List<Product>>> partionedEntrySet = partionedMap.entrySet();
        for (Entry<Boolean, List<Product>> entry : partionedEntrySet)
        {
            System.out.println(entry.getKey() +" = "+entry.getValue());
        }
        
    }

}
Output
20 = [Product [name=lemon, price=20], Product [name=bread, price=20]]
30 = [Product [name=sugar, price=30]]
15 = [Product [name=potatoes, price=15], Product [name=orange, price=15]]
---------------------------------------------------
false = [Product [name=potatoes, price=15], Product [name=orange, price=15], 
        Product [name=lemon, price=20], Product [name=bread, price=20]]
true = [Product [name=sugar, price=30]]

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/adf72ea7a150b1a34af5820c19f2232b522614b9/BasicJava/StreamDemo_Collectors_group/?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