Wednesday, 9 May 2018

1.      Java Stream Example : reduce() Method in Collection

2.      Java Stream Example: Sum by using Collectors Methods

3.      Java Stream Example: Find Max and Min Product Price

4.      Java Stream Example: count() Method in Collection




package streamconcept;

public class ConsumerProduct {
               
                int cpId;
                String cpName;
                float cpprice;
                public ConsumerProduct(int cpId, String cpName, float cpprice) {
                                super();
                                this.cpId = cpId;
                                this.cpName = cpName;
                                this.cpprice = cpprice;
                }
               
               

}


package streamconcept;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class ConsumerProductMain {
               
                public static void main(String[] args) {
                                List<ConsumerProduct> l=new ArrayList<>();
                                ConsumerProduct cp1=new ConsumerProduct(101, "cooler", 15000.00f);
                                ConsumerProduct cp2=new ConsumerProduct(102, "laptop", 45000.00f);
                                ConsumerProduct cp3=new ConsumerProduct(103, "mobile", 10000.00f);
                                ConsumerProduct cp4=new ConsumerProduct(104, "ac", 19000.00f);
                                ConsumerProduct cp5=new ConsumerProduct(105, "inverter", 30000.00f);
                                l.add(cp1);
                                l.add(cp2);
                                l.add(cp3);
                                l.add(cp4);
                                l.add(cp5);
                               
                                System.out.println("-------summation of all price ist way-----------");
                               
                                //ist way
                               
                                float totalprice=l.stream()
                                                                .map(p->p.cpprice)
                                                                .reduce(0.0f,(sum,price)->sum+price);  // accumulating price 
                                        System.out.println(totalprice);
                                       
                                        System.out.println("-------summation of all price 2nd way-----------");
                                        //2nd way
                                       
                                        float totalprice1=l.stream()
                                                                .map(p->p.cpprice)
                                                                .reduce(0.0f, Float::sum);// accumulating price, by referring method of Float class
                                        System.out.println(totalprice1);
                                       
                                        System.out.println("-------summation of all price 3rd way-----------");
                                        //3rd way
                                       
                                        double totalprice2=l.stream()
                                                                .collect(Collectors.summingDouble(p->p.cpprice));
                                        System.out.println(totalprice2);
                                       
                                        //find maximum price and minimum price
                                        System.out.println("-----maximum price--------------");
                                       ConsumerProduct maxprice = l.stream()
                                        .max((p1,p2)->
                                        p1.cpprice>p2.cpprice?1:-1).get();
                                       System.out.println(maxprice.cpprice);
                                      
                                       System.out.println("-----minimum price--------------");
                                       ConsumerProduct minprice = l.stream()
                                                                        .min((p1,p2)->
                                                                        p1.cpprice<p2.cpprice?-1:1).get();
                                                                       System.out.println(minprice.cpprice);
                                                                      
                                                                      
                                                                       System.out.println("--------------count() demo--------");
                                                                      long count =l.stream()
                                                                                                  .filter(p->p.cpprice<=15000.00f)
                                                                                                  .count();
                                                                      System.out.println(count);
                                      
                                       
                                       
                                       
                                       
                                                               
                               
                }

}


o/p:--------------------

-------summation of all price ist way-----------
119000.0
-------summation of all price 2nd way-----------
119000.0
-------summation of all price 3rd way-----------
119000.0
-----maximum price--------------
45000.0
-----minimum price--------------
10000.0
--------------count() demo--------

2








No comments:

Post a Comment