Passing named functions as arguments

Java 8 added lambda expressions. Using lambdas similarly to anonymous classes is pretty straight forward, but I wonder if there is a related functionality for using named functions as arguments to other functions. For example, is there a Java way to write the following Python code:

list1 = (1,2,3) list2 = (4,5,6) def add(a, b): return a+b for e in map(add, list1, list2): print(e) 

Exit

 5 7 9 
+5
source share
4 answers

Yes, you can use method references, such as Integer::sum , where lambdas are allowed.

 int six = IntStream.of(1, 2, 3) .reduce(0, Integer::sum); 

It is equivalent

 int six = IntStream.of(1, 2, 3) .reduce(0, (a, b) -> Integer.sum(a, b)); 

Methods like Integer.sum and Double.max were added in Java 8 just so that they could be used in lambdas like this.

There is no built-in way to “archive” multiple lists together, as Python does.

+4
source

Well, in Java there are no “functions”, only methods, so you won’t get the same.

The best way to achieve this is to use the recommendations of the method , in which case you can use BiConsumer :

 import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.function.BiConsumer; public class StackOverflow { public static void main(String[] args) throws InterruptedException { List<Integer> list1 = new ArrayList<>(); list1.add(1); list1.add(2); list1.add(3); List<Integer> list2 = new ArrayList<>(); list2.add(4); list2.add(5); list2.add(6); mapOver(StackOverflow::add, list1, list2); } public static void add(Integer a, Integer b) { System.out.println(a + b); } public static <T> void mapOver(BiConsumer<T, T> function, Iterable<T> first, Iterable<T> second) { final Iterator<T> firstIterator = first.iterator(); final Iterator<T> secondIterator = second.iterator(); while (firstIterator.hasNext() && secondIterator.hasNext()) { function.accept(firstIterator.next(), secondIterator.next()); } } } 

Conclusion:

 5 7 9 
+2
source

Not sure what your question really is, but in Java it will be something like.

 List<Integer list1 = Arrays.asList(1,2,3); List<Integer list2 = Arrays.asList(4,5,6); IntStream.range(0, list1.size()) .parallel() .forEach(i -> { System.out.println(list1.get(i) + list2.get(i)); }); 

.. since you cannot pass multiple sets in Java as you can in python.

0
source

Of course, you can declare variables whose types are functional interfaces, and then assign them lambdas. This is the conclusion of this answer and its comment. For example, you might have this declaration:

 BiFunction<Integer,Integer,Integer> add = (a, b) -> a + b; 

This is not exactly what the OP asked for. The Python equivalent of this would be something like this:

 add = lambda a, b: a + b 

This is not a named function; this is a variable whose value is (lambda function). The initial question was about using a named function in Java. A way to do this is with a method reference.

Suppose you have a method that takes a BiFunction<Integer,Integer,Integer> parameter. You can pass it the add variable as described above. But you can also declare a regular method and use it as the target of a method reference. The trick is to make sure that the "form" (number and type of arguments, return type) matches any functional interface that you are trying to use. In this case, you have two parameters of type Integer , and the return value is Integer , so your method will be

 static Integer add(Integer a, Integer b) { return a + b; } 

You can then reference it using the method link of the form MyClass::add .

Note that I made this a static method. If it was an instance method, the receiver ( this ) is a hidden parameter that must be considered when matching it with the functional interface.

Please also note that due to automatic boxing you can also write:

 static int add(int a, int b) { return a + b; } 

Of course, it is used to assign lambdas to variables, for example, if an algorithm is selected at run time. But if all lambdas are bound to constant fields, it is often easier to use references to methods designed for regular methods.

0
source

All Articles