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#map(java.util.function.Function)
* @see Stream#sorted()
* @see Stream#collect(java.util.stream.Collector)
*
*/
public void operation_statefull_order(List<Product> source) {
List<Float> rPrize = source.stream().map( product -> product.price ).sorted().collect( Collectors.toList() );
System.out.println( rPrize );
}
public void operation_statefull_distinct(List<Product> source) {
List<Float> rPrize = source.stream().map( product -> product.price ).distinct().collect( Collectors.toList() );
System.out.println( rPrize );
}
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 :: ordina elementi -> nuovo flusso
new UniTest().operation_statefull_order(UniTest.createTest());
// Unit test - sorgente input -> pipeline :: elementi senza duplicati -> nuovo flusso
new UniTest().operation_statefull_distinct(UniTest.createTest());
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: