fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.Map;
  7. import java.util.function.BiFunction;
  8.  
  9. /* Name of the class has to be "Main" only if the class is public. */
  10. class Ideone
  11. {
  12. public static void main(String[] args) {
  13. BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;//lambda a, b : a + b
  14. BiFunction<Integer, Integer, Integer> sub = (a, b) -> a - b;//lambda a, b : a - b
  15.  
  16. //then create a new Map which take the sign and the correspondence BiFunction
  17. Map<String, BiFunction> signs = new HashMap<>();
  18. signs.put("+", add);
  19. signs.put("-", sub);
  20.  
  21. int a = 5;
  22. int b = 3;
  23.  
  24. //loop over the sings map and apply the operation
  25. signs.values().forEach(v-> System.out.println(v.apply(a, b)));
  26. }
  27. }
Success #stdin #stdout 0.17s 2841600KB
stdin
Standard input is empty
stdout
8
2