Java Simple Solution with functional Interface


#1

import java.util.function.IntBinaryOperator;

public class Solution {
public int evalRPN(ArrayList A) {

    int i=0;
    Stack<Integer> s = new Stack<>();
    HashMap<String, IntBinaryOperator> h = new HashMap<>();
    h.put("+", (a,b) -> a+b);
    h.put("*", (a,b) -> a*b);
    h.put("/", (a,b) -> a/b);
    h.put("-", (a,b) -> a-b);
    
    while(i<A.size()) {
        if(!h.containsKey(A.get(i))) {
            s.add(Integer.parseInt(A.get(i)));
        } else {
            int b = s.pop();
            int a = s.pop();
            s.push(h.get(A.get(i)).applyAsInt(a,b));
        }
        i++;
    }
    
    return s.peek();
}

}