8.6
Assignment 17: Optionals
Goals: Practice working with optionals and anonymous classes.
You should submit one .java file containing the solution to this problem.
Be sure to properly test your code and write purpose statements for your methods. A lack of tests and documentation will result in a lower grade! Remember that testing requires you to make some examples of data in an examples class.
Optional Utils
For this problem set, you should finish implementing the following class. Whenever appropriate, write methods in terms of each other and use the new syntaxes we saw in class to make your code as crisp as possible. If any of the interface parameters are unfamiliar to you, look them up in the docs. As always, thoroughly test your methods.
import java.util.List; import java.util.Optional; import java.util.function.Function; import java.util.function.Predicate; class OptionalUtils { // return the list of f applied to every element in xs, but do not include the empty results static <X, Y> List<Y> convertPassing(List<X> xs, Function<X, Optional<Y>> f) { return null; } // return the list of f applied to every element in xs, but do not include the results that don't pass pred static <X, Y> List<Y> convertPassing(List<X> xs, Function<X, Y> f, Predicate<Y> pred) { return null; } // drop all empty values static <X> List<X> dropEmpty(List<Optional<X>> xs) { return null; } // return a new function that applies f to non-null values and returns empty optionals // on null values static <X, Y> Function<X, Optional<Y>> nullFriendly(Function<X, Y> f) { return null; } // apply and to the boolean values if both are present, otherwise return empty optional static Optional<Boolean> optionalAnd(Optional<Boolean> b1, Optional<Boolean> b2) { return null; } }