Reputation: 143
while looking at a simple Comparator usage example:
Comparator<Developer> byName = new Comparator<Developer>() {
@Override
public int compare(Developer o1, Developer o2) {
return o1.getName().compareTo(o2.getName());
}
I'm having trouble understanding this method declaration I've never seen such thing before.
In addition, I tried to do something else: I created a class that have inner classes that implement Comparator:
class Test{
class A implements Comparator<Integer> {
@Override
public int compare(int a, int b) {
// Do something
}
}
class B implements Comparator<Integer> {
@Override
public int compare(int a, int b) {
return a > b ? 1 :A.compare(a, b);
}
}
}
A.compare(a,b) doesn't compile. It asks me to to make A and compare static but then I don't override the method compare of the comparator class.
Plus, saying I want to use the method A in another class
class C {
private int foo(){
Test.A()
}
}
it doesn't work also how do I do that properly? Any suggestions?
Upvotes: 0
Views: 55
Reputation: 40388
When you use a Comparator<Something>
, then you are using a parameterized type (in this case: Something
).
The method signiture of compare
needs to match this type.
So, try:
public int compare(Integer a, Integer b)
Upvotes: 2