Wednesday, 20 September 2017

Java 8 Stream[Sequential and Parallel Stream V2] | Java 8 streams tutorial | Streams in Java 8


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=VOFbMhy27Io&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.stream.Stream;

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));

        /*
         * Before Java 8, parallelization was complex. Emerging of the
         * ExecutorService and the ForkJoin simplified developer’s
         * life a little bit, but they still should keep in mind how
         * to create a specific executor, how to run it and so on.
         * Java 8 introduced a way of accomplishing parallelism in a
         * functional style.
         * 
         * The API allows creating parallel streams, which perform
         * operations in a parallel mode. When the source of a stream
         * is a Collection or an array it can be achieved with the
         * help of the parallelStream() method:
         * 
         * Under the hood, Stream API automatically uses the ForkJoin
         * framework to execute operations in parallel. By default,
         * the common thread pool will be used and there is no way (at
         * least for now) to assign some custom thread pool to it.
         */

        Stream<Product> stream = productList.parallelStream();
        boolean isParallel = stream.isParallel();

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

        boolean bigPrice = stream
                .map(product -> product.getPrice())
                .anyMatch(price -> price > 25);

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

    }

}
Output
isParallel = true
bigPrice = true

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

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

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