I'm writing a simple Predicate function as follows:
public static Predicate<String> stringPredicate(String op, String compVal) {
if (op.equals("=")){
return p -> p.compareTo(compVal) == 0;
}
else if (op.equals("<")){
return p -> p.compareTo(compVal) == -1;
}
else if (op.equals("<=")){
return p -> p.compareTo(compVal) == 0 || p.compareTo(compVal) == -1;
}
else if (op.equals(">")){
return p -> p.compareTo(compVal) == 1;
}
else if (op.equals(">=")){
return p -> p.compareTo(compVal) == 1 || p.compareTo(compVal) == 0;
}
return p -> -1 == 0; //return false
}
I'd like this function to work for both Integers, Floats and Strings. So, rather than writing 2 identical predicate functions, one for Strings and one for Integer+Float, I'd like to write a single one as compareTo() method works for all 3 of them. I guess I can do it by replacing
Predicate<String>
with Predicate<Super class of Integer/Float/String>
. I tried Object
but that didn't work. So, what should I put instead of <String>
to use the function in the way I want?