Wednesday, 9 May 2018


Java Example: Filtering Collection without using Stream


package streamconcept;

public class Product {
            int pid;
            String pname;
            float pprice;
            public Product(int pid, String pname, float pprice) {
                        super();
                        this.pid = pid;
                        this.pname = pname;
                        this.pprice = pprice;
            }
           

}




package streamconcept;

import java.util.ArrayList;
import java.util.List;

public class ProductMain {
               
                public List<Product> getData()
                {
     List<Product> l=new ArrayList<>();
     Product p1=new Product(101, "laptop", 45000.00f);
     Product p2=new Product(102, "computer", 35000.00f);
     Product p3=new Product(103, "mobile", 15000.00f);
     Product p4=new Product(104, "cooler",5000.00f);
     l.add(p1);
     l.add(p2);
     l.add(p3);
     l.add(p4);
     return l;
   
                }
                }



package streamconcept;

import java.util.ArrayList;
import java.util.List;

public class ProductClient {
               
                public static void main(String[] args) {
                                ProductMain pm=new ProductMain();
                                List<Product> gd = pm.getData();
                                List<Float> price=new ArrayList<>();
                                for(Product p1:gd)
                                {
                                                if(p1.pprice>30000)
                                                {
                                                                price.add(p1.pprice);
                                                }
                                }
                               
                                System.out.println(price);
                }

}



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

[45000.0, 35000.0]


No comments:

Post a Comment