import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* {@link <a href=
* "https://[Log in to view URL]"
* target= "_blank"></a>}
*
* @author itammb ( Italia Massimiliano Buscati )
* @version JDK 1.15
*
*/
class Main {
private static class Product {
private int id;
private String name;
private float price;
@Override
public String toString() {
return "Product [id=" + id + ", name=" + name + ", price=" + price + "]";
}
private Product(int id, String name, float price) {
this.id = id;
this.name = name;
this.price = price;
}
}
private static class UniTest {
/**
* @see Stream#filter(java.util.function.Predicate)
* @see Stream#forEach(java.util.function.Consumer)
*
*/
public void operation_stateless_filter(List<Product> source) {
Stream<Product> productS = source.stream().filter(product -> product.price > 30000); // <- operazioni
// intermedie
productS.forEach(product -> System.out.println(product)); // <- operazioni terminale
}
/**
* @see Stream#map(java.util.function.Function)
* @see Stream#forEach(java.util.function.Consumer)
*
*/
public void operation_stateless_map(List<Product> source) {
Stream<Float> prizeS = source.stream().map(product -> product.price); // <- operazioni intermedie
prizeS.forEach(prize -> System.out.println(prize)); // <- operazioni terminale
}
/**
* @see Stream#map(java.util.function.Function)
* @see Stream#collect(java.util.stream.Collector)
* @see Collectors#toList()
*
*/
public void operation_stateless_collect(List<Product> source) {
List<Float> rPrize = source.stream().map(product -> product.price).collect(Collectors.toList()); // <-
// operazioni
// terminale-raccolta
System.out.println(rPrize);
}
public void operation_stateless_many_operations(List<Product> source) {
List<Float> rPrize = source.stream().filter(product -> product.price > 30000).map(product -> product.price)
.collect(Collectors.toList());
System.out.println(rPrize);
}
public void operation_stateless_navigation(List<Product> source) {
source.stream().filter( product -> product.price == 30000 ).forEach( product -> System.out.println( product ) );
}
public static List<Product> createTest() {
// elementi in serie
return new ArrayList<Product>() {
private static final long serialVersionUID = -69L;
{
add(new Product(1, "HP Laptop", 25000f));
add(new Product(2, "Dell Laptop", 30000f));
add(new Product(3, "Lenevo Laptop", 28000f));
add(new Product(4, "Sony Laptop", 28000f));
add(new Product(5, "Apple Laptop", 90000f));
}
};
}
}
public static void main(String args[]) {
// Unit test - sorgente input -> pipeline :: filtro -> nuovo flusso
new UniTest().operation_stateless_filter(UniTest.createTest());
// Unit test - sorgente input -> pipeline :: estrazione elemento -> nuovo flusso
new UniTest().operation_stateless_map(UniTest.createTest());
// Unit test - sorgente input -> pipeline :: raccolta elemeno -> nuova sorgente
// elaborazione in un unico passaggio
new UniTest().operation_stateless_collect(UniTest.createTest());
// Unit test - sorgente input -> pipeline :: composizione di più operazioni
// stateless -> nuova sorgente
new UniTest().operation_stateless_many_operations(UniTest.createTest());
// Unit test - navigazione -> nuovo flusso
new UniTest().operation_stateless_navigation(UniTest.createTest());
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: