Java Stream filter() example (java-8)
In the following example, we are
fetching and iterating filtered data.
package
govind;
public class
Product {
int pid;
String pname;
float price;
public int
getPid() {
return pid;
}
public void
setPid(int pid) {
this.pid = pid;
}
public
String getPname() {
return pname;
}
public void
setPname(String pname) {
this.pname = pname;
}
public float
getPrice() {
return price;
}
public void
setPrice(float price) {
this.price = price;
}
}
package govind;
import
java.util.ArrayList;
import
java.util.List;
public
class ProductMain {
public
static void
main(String[] args) {
List<Product> l=new
ArrayList<>();
Product p1=new
Product();
p1.setPid(101);
p1.setPname("mobile");
p1.setPrice(10000.00f);
l.add(p1);
Product p2=new
Product();
p2.setPid(102);
p2.setPname("laptop");
p2.setPrice(50000.00f);
l.add(p2);
Product p3=new
Product();
p3.setPid(103);
p3.setPname("cooler");
p3.setPrice(50000.00f);
l.add(p3);
Product p4=new
Product();
p4.setPid(104);
p4.setPname("computer");
p4.setPrice(33000.00f);
l.add(p4);
Product p5=new
Product();
p5.setPid(105);
p5.setPname("printer");
p5.setPrice(12000.00f);
l.add(p5);
l.stream( )
.filter(p->p.getPrice()>30000.00f)//filtering price
.map(pm->pm.getPrice())//fetching price
.forEach(System.out::println);//iterating
price
}
}
o/p:------------------
50000.0
50000.0
33000.0
No comments:
Post a Comment