Java: How to use the Function interface

I'll show you how you can use the Function interface. The Function interface returns an Object of any type.

public static List<String> createProductsDescription(List<Product> products,
Function<Product, String> function){
List<String> descriptions = new ArrayList<>();
for(Product p : products){
descriptions.add( function.apply(p) );
}
return descriptions;
}


The code that calls the createProductDescription uses lambda expression :

List<String> productsDescription = createProductsDescription(products,
p -> {
return p.getName() + ": " + p.getCompanyName();
});


Comments

Popular posts from this blog

Spring JPA : Using Specification with Projection

Chip input using Reactive Form