concept Function in category java

appears as: Function, Function
Modern Java in Action: Lambdas, streams, reactive and functional programming

This is an excerpt from Manning's book Modern Java in Action: Lambdas, streams, reactive and functional programming.

  • Function— We say function because a lambda isn’t associated with a particular class like a method is. But like a method, a lambda has a list of parameters, a body, a return type, and a possible list of exceptions that can be thrown.
  • Listing 3.4. Working with a Function
    @FunctionalInterface
    public interface Function<T, R> {
        R apply(T t);
    }
    public <T, R> List<R> map(List<T> list, Function<T, R> f) {
        List<R> result = new ArrayList<>();
        for(T t: list) {
            result.add(f.apply(t));
        }
        return result;
    }
    // [7, 2, 6]
    List<Integer> l = map(
                           Arrays.asList("lambdas", "in", "action"),
                           (String s) -> s.length()                     #1
                   );

    4 Using DoubleFunction<Double> is more efficient than using Function<Double,Double> as it avoids boxing the result.

    Figure 19.5. Elements of a LinkedList exist (are spread out) in memory. But elements of a LazyList are created on demand by a Function; you can see them as being spread out in time.
    Java 8 in Action: Lambdas, streams, and functional-style programming

    This is an excerpt from Manning's book Java 8 in Action: Lambdas, streams, and functional-style programming.

  • Function— We say function because a lambda isn’t associated with a particular class like a method is. But like a method, a lambda has a list of parameters, a body, a return type, and a possible list of exceptions that can be thrown.
  • Listing 3.4. Working with a Function
    @FunctionalInterface public interface Function<T, R>{ R apply(T t); } public static <T, R> List<R> map(List<T> list, Function<T, R> f) { List<R> result = new ArrayList<>(); for(T s: list){ result.add(f.apply(s)); } return result; } // [7, 2, 6] List<Integer> l = map( Arrays.asList("lambdas","in","action"), (String s) -> s.length() #1 ); #1: The lambda is the implementation for the apply method of Function.
    Figure 14.5. Elements of a LinkedList exist (are spread out) in memory. But elements of a LazyList are created on demand by a Function—you can see them as spread out in time.