/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Map;
import java.util.function.BiFunction;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main
(String[] args
) { BiFunction
<Integer,
Integer, Integer
> add
= (a, b
) -> a
+ b
;//lambda a, b : a + b BiFunction
<Integer,
Integer, Integer
> sub
= (a, b
) -> a
- b
;//lambda a, b : a - b
//then create a new Map which take the sign and the correspondence BiFunction
Map
<String, BiFunction
> signs
= new HashMap
<>(); signs.put("+", add);
signs.put("-", sub);
int a = 5;
int b = 3;
//loop over the sings map and apply the operation
signs.
values().
forEach(v
-> System.
out.
println(v.
apply(a, b
))); }
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CmltcG9ydCBqYXZhLnV0aWwuTWFwOwppbXBvcnQgamF2YS51dGlsLmZ1bmN0aW9uLkJpRnVuY3Rpb247CgovKiBOYW1lIG9mIHRoZSBjbGFzcyBoYXMgdG8gYmUgIk1haW4iIG9ubHkgaWYgdGhlIGNsYXNzIGlzIHB1YmxpYy4gKi8KY2xhc3MgSWRlb25lCnsKCXB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKCSAgICBCaUZ1bmN0aW9uPEludGVnZXIsIEludGVnZXIsIEludGVnZXI+IGFkZCA9IChhLCBiKSAtPiBhICsgYjsvL2xhbWJkYSBhLCBiIDogYSArIGIKCSAgICBCaUZ1bmN0aW9uPEludGVnZXIsIEludGVnZXIsIEludGVnZXI+IHN1YiA9IChhLCBiKSAtPiBhIC0gYjsvL2xhbWJkYSBhLCBiIDogYSAtIGIKCQoJICAgIC8vdGhlbiBjcmVhdGUgYSBuZXcgTWFwIHdoaWNoIHRha2UgdGhlIHNpZ24gYW5kIHRoZSB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