For a simple condition comparison
import java.util.*;
class Product{
int id;
String name;
float price;
public Product(int id, String name, float price) {
this.id = id;
this.name = name;
this.price = price;
}
}
public class JavaStreamExample {
public static void main(String[] args) {
List productsList = new ArrayList();
//Adding Products in the productlist
productsList.add(new Product(1," redmi note 10",25000f ));
productsList.add(new Product(2," asus roug",30000f));
productsList.add(new Product(3," samsung note 10 lite ",28000f ));
productsList.add(new Product(4, “ Sony Experia 3",28000f));
productsList.add(new Product(5," Apple iphone 12 pro max ",90000f ));
List productPriceList = new ArrayList();
for(Product product: productsList){
if(product.price<30000){
productPriceList.add(product.price);
}
}
System.out.println(productPriceList); // display data
}
}
Output
For iteration
import java.util.stream.*;
public class JavaStreamExample {
public static void main(String[] args){
Stream.iterate(1, element->element+1)
.filter(element -> element%5==0)
.limit(5)
.forEach(System.out::println);
}
}
Output
To find sum
import java.util.*;
import java.util.stream.Collectors;
class Product{
int id;
String name;
float price;
public Product(int id, String name, float price) {
this.id = id;
this.name = name;
this.price = price;
}
}
public class JavaStreamExample {
public static void main(String[] args) {
List productsList = new ArrayList();
//Adding Products in the list
productsList.add(new Product(1," HP spector x360 Laptop",25000f ));
productsList.add(new Product(2," Dell inspiron Laptop",30000f ));
productsList.add(new Product(3," Lenovo legion Laptop",28000f ));
productsList.add(new Product(4," Sony Laptop",28000f));
productsList.add(new Product(5," Apple mac book pro Laptop",90000f ));
// Using Collectors's method to sum the prices.
double totalPrice3 = productsList.stream()
.collect(Collectors.summingDouble(product -> product.price ));
System.out.println(totalPrice3);
}
}
Output
For count() method
import java.util.*;
class Product{
int id;
String name;
float price;
public Product(int id, String name, float price) {
this.id = id;
this.name = name;
this.price = price;
}
}
public class JavaStreamExample {
public static void main(String[] args) {
List productsList = new ArrayList();
//Adding Products in the list
productsList.add(new Product(1," HP Laptop",25000f ));
productsList.add(new Product(2," Dell xps Laptop",30000f ));
productsList.add(new Product(3," Lenevo yoga book Laptop",28000f ));
productsList.add(new Product(4," Sony Laptop",28000f));
productsList.add(new Product(5," Apple mac book Laptop ",90000f ));
// count number of products based on the filter
long count = productsList.stream()
.filter(product -> product.price<30000) //filter of data
.count();
System.out.println(count);
}
}
Output
For filtering data
import java.util.*;
import java.util.stream.Collectors;
class Product{
int id;
String name;
float price;
public Product(int id, String name, float price) {
this.id = id;
this.name = name;
this.price = price;
}
}
public class JavaStreamExample {
public static void main(String[] args) {
List productsList = new ArrayList();
//Adding Products in the list
productsList.add(new Product(1," HP Laptop",25000f ));
productsList.add(new Product(2," Dell Laptop",30000f ));
productsList.add(new Product(3," Lenevo Laptop",28000f ));
productsList.add(new Product(4," Sony Laptop",28000f ));
productsList.add(new Product(5," Apple Laptop",90000f ));
List productPriceList2 = productsList.stream()
.filter(p -> p.price > 30000)// filtering data
.map(p->p.price) // fetching price
.collect(Collectors.toList()); // collecting as list
System.out.println(productPriceList2);
}
}
Output