1

I need to compare two double values with relation type as string. Eg:

String relation = "<=";
   double aAA=5.9,bBB=6.999999;

In the above example need to compare aAA relation bBB using java

Shiva Goud A
  • 322
  • 1
  • 6
  • 18

1 Answers1

1

You could create a Map mapping the Strings to the actual operations, e.g. as BiPredicate.

Map<String, BiPredicate<Double, Double>> relations = new HashMap<>();
relations.put("<=", (a, b) -> a <= b);
relations.put(">=", (a, b) -> a >= b);
relations.put("==", (a, b) -> a == b);
// ...

String relation = "<=";
double aAA=5.9,bBB=6.999999;

boolean result = relations.get(relation).test(aAA, bBB);
Ward
  • 2,799
  • 20
  • 26