Access
Access

Reputation: 241

Parse String into Comparator Type in Java

Given the Strings

String str1 = "==";
String str2 = "<=";
String str3 = "!=";
...

Is there a way to parse those Strings into their corresponding comparator types to use them directly in a comparison (like "comparator.parseComparator(str1)")? Or do i need to find a different way?

Upvotes: 1

Views: 604

Answers (1)

user784540
user784540

Reputation:

Make a map<String, Comparator>

where the key is the operator you want to apply, and the object is the relevant Comparator instance.

Get the relevant comparator by key and apply it whenever you want.

Upvotes: 5

Related Questions