Wednesday, 9 May 2018


//How to use Stream in java?

package conversionlist;

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

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, "laptop", 45000.00f);
                                ConsumerProduct cp2=new ConsumerProduct(102, "computer", 25000.00f);
                                ConsumerProduct cp3=new ConsumerProduct(103, "cooler", 5000.00f);
                                ConsumerProduct cp4=new ConsumerProduct(104, "ac", 19000.00f);
                                ConsumerProduct cp5=new ConsumerProduct(105, "mobile", 15000.00f);
                                l.add(cp1);
                                l.add(cp2);
                                l.add(cp3);
                                l.add(cp4);
                                l.add(cp5);
                                List<Float> price = l.stream()
                                                                .filter(p->p.cpPrice>10000.00f)
                                                                .map(m->m.cpPrice)
                                        .collect(Collectors.toList());
                                        System.out.println(price);
                                       
                                       
                                       
                               
                }}

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

[45000.0, 25000.0, 19000.0, 15000.0]

No comments:

Post a Comment