I want to convert some String to an operator like this:
int value = 1;
int valueToCompare = 3;
String operation = \"<\";
if (value operation
you may try this:
import java.util.*;
interface Operator {
boolean compare(int a, int b);
}
class Launch
{
public static void main (String[] args) throws java.lang.Exception
{
Map opMap = new HashMap();
opMap.put(">", new Operator() {
@Override public boolean compare(int a, int b) {
return a > b;
}
});
opMap.put("<", new Operator() {
@Override public boolean compare(int a, int b) {
return a < b;
}
});
opMap.put("==", new Operator() {
@Override public boolean compare(int a, int b) {
return a == b;
}
});
String op = ">";
int i = 4, j = 5;
boolean test = opMap.get(op).compare(i, j);
System.out.printf("test: %b, i: %d, op: %s, j: %d\n", test, i, op, j);
//prints: test: false, i: 4, op: >, j: 5
}
}