Java Reference
In-Depth Information
}
}
return results;
}
Predicate<String> nonEmptyStringPredicate = (String s) -> !s.isEmpty();
List<String> nonEmpty = filter(listOfStrings, nonEmptyStringPredicate );
If you look up the Javadoc specification of the Predicate interface, you may notice additional
methods such as and and or. Don't worry about them for now. We come back to these in section
3.8 .
3.4.2. Consumer
The java.util.function.Consumer<T> interface defines an abstract method named accept that
takes an object of generic type T and returns no result (void). You might use this interface when
you need to access an object of type T and perform some operations on it. For example, you can
use it to create a method forEach, which takes a list of Integers and applies an operation on each
element of that list. In the following listing you use this forEach method combined with a
lambda to print all the elements of the list.
Listing 3.3. Working with a Consumer
3.4.3. Function
The java.util.function.Function<T, R> interface defines an abstract method named apply that
takes an object of generic type T as input and returns an object of generic type R. You might use
 
Search WWH ::




Custom Search