13. Higher Order of Functionality
users.stream() .filter(User.BORN.between(1985, 1995)) // Filters out Users born 1985 up to and including 1994 .map(User.CATEGORY.setTo(3)) // Applies a function that sets their category to 3 .forEach(users.updater()); // Applies the updater function to the selected users
The code snippet above will;
a) extract users from an underlying database where the users are born between 1985 and 1995 (and only those users)
b) for each such user, it will apply a mapping from a user to an updated user where the category has been set to 3 (but all other fields remain the same)
c) for each updated user, a database updater method will be applied that will result in the updated user being persisted in the database.
So, the snippet above is a sequence of methods that are provided other methods as per the paradigm of Higher Order of Functionality. If we later elect to store our data not in a database but in a file, in memory or even in an Excel diagram, then we only need to provide another set of functions. The stream logic will remain exactly the same.
Follow the Java Holiday Calendar 2016 with small tips and tricks all the way through the winter holiday season.