List generalizations with an interface - java

i has the following interface:

public interface Evaluator<T> { T evaluate(Collection<Extractable> sample); } 

And a class that implements this interface:

 public class PrecisionEvaluator implements Evaluator<Map<Triple<Short, Integer, String>, AttributePrecisionBean>> { @Override public Map<Triple<Short, Integer, String>, AttributePrecisionBean> evaluate(Collection<Extractable> sample){ ... 

}

so using the above will be:

 Evaluator<Map<Triple<Short, Integer, String>, AttributePrecisionBean>> eval = new PrecisionEvaluator(); 

Is there a way to make the Evaluator interface implicitly return the generic type defined in the class that implements it? so using will be simple:

 Evaluator eval = new PrecisionEvaluator(); 

Or is there a cleaner way to do this?

+5
source share
2 answers

As far as I know, an object of type Evaluator will be a raw type unless you specify a generic type. What you can do, since Java 7 is the type of output for creating a shared instance (although in this case it probably won't help):

 Evaluator<Map<Triple<Short, Integer, String>, AttributePrecisionBean>> eval = new Evaluator<>(); 
0
source

Yes, there is a way to do this: learn Scala, its syntax is much simpler than Java, thanks to, among other things, the output of the compiler type.

More seriously, you cannot do better with Java, if you use only Evaluator , it will be compiled as Evaluator<Object> , which will not allow you to have type safety and polymorphism. All you can do here is create a class class CustomTriple extends Triple<Short,Integer,String>> to have something shorter.

0
source

Source: https://habr.com/ru/post/1205947/


All Articles