Java: How to use the Function interface
I'll show you how you can use the
The code that calls the
Function
interface. The Function
interface returns an Object
of any type.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 :
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<String> productsDescription = createProductsDescription(products, | |
p -> { | |
return p.getName() + ": " + p.getCompanyName(); | |
}); |
Comments
Post a Comment