Static methods for SAM types

Brian Goetz brian.goetz at oracle.com
Sun Apr 14 17:27:24 PDT 2013


As of Java 8 we can have static methods in interfaces.  Here's a small 
set of static methods for the java.util.function SAM types.

Function:
     public static <T> Function<T, T> identity()

Actually, that's the end of my must-have list!

But there have also been some others suggested, here's a sampling.  Do 
any of these speak to anyone?

Predicate:

     // Like o::isEquals, but also works if target is null
     public static<T> Predicate<T> isEqual(Object target)

Function:

     static <T> Function<T, T> substitute(T subOut, T subIn) {
         return t -> Objects.equals(subOut, t) ? subIn : t;
     }

     static <T, R> Function<T, R> constant(R constant) {
         return t -> constant;
     }

     // Or could be default Predicate.asFunction(forTrue, forFalse)
     static<T, R> forPredicate(Predicate<T>, R forTrue, R forFalse)

     // Like map::get, but throws if not present
     static <R, T> Function<T, R> forMap(Map<? super T, ? extends R> map)



More information about the lambda-libs-spec-experts mailing list