Marco Dinatsoli
Marco Dinatsoli

Reputation: 10580

java calling a generic static method

I am trying to make a generic method to compare objects from many types.

This is my simple interface

interface comparable<T> {
    boolean isBiggerThan(T t1);
}

and this is one implementation for it:

class StringComparable implements comparable<String> {

    public StringComparable(String s) {
        this.s = s;
    }

    String s;

    @Override
    public boolean isBiggerThan(String t1) {
        return s.equals(t1);
    }

}

this is my class that has a generic method:

class Utilt {
    public static <T extends comparable<T>> int biggerThan(T values[], T t) {
        int count = 0;
        for (T oneValue : values) {
            if (oneValue.isBiggerThan(t)) {
                count++;
            }
        }
        return count;
    }
}

I call that method like this:

public class TestTest {
    public static void main(String args[]) {
        StringComparable a = new StringComparable("Totti");
        StringComparable pi = new StringComparable("Pirlo");
        int i = Utilt.biggerThan(new StringComparable[] { a, pi }, a);
    }
}

But I get this error:

The method `biggerThan(T[], T)` in the type `Utilt` is not applicable for the arguments `(StringComparable[], StringComparable)`

Upvotes: 1

Views: 498

Answers (3)

Sergii Bishyr
Sergii Bishyr

Reputation: 8641

Pay attention that !comparable<String> instanceof comparable<StringComparable>. So try something like this:

class StringComparable implements comparable<StringComparable> {

  public StringComparable(String s) {
    this.s = s;
  }

  String s;

  public String getS(){
    return s;
  }

  @Override
  public boolean isBiggerThan(StringComparable t1) {
    return s.equals(t1.getS());
  }

}

And Utilt class

class Utilt {
    public static <T extends comparable<T>> int biggerThan(T values[], T t) {
        int count = 0;
        for (T oneValue : values) {
            if (oneValue.isBiggerThan(t)) {
                count++;
            }
        }
        return count;
    }
}

Also is better to override equals and hashCode, so you can call this.equals(t1) in isBiggerThan method.

BTW, method isBiggerThan looks very suspicious as long as:

StringComparable s1 = new StringComparable("string1");
StringComparable s2 = new StringComparable("string2");
s1.isBiggerThan(s2); // output => true
s2.isBiggerThan(s1); // output => true

It's up to you, but for me this logic looks wrong.

Upvotes: 0

Eran
Eran

Reputation: 393846

The generic parameter T of your static method biggerThan must implement comparable<T>. StringComparable implements comparable<String>, not comparable<StringComparable>, so it doesn't match your static method's generic parameter.

You can fix this by changing StringComparable to implement comparable<StringComparable>.

class StringComparable implements comparable<StringComparable> {

    public StringComparable(String s) {
        this.s = s;
    }

    String s;

    @Override
    public boolean isBiggerThan(StringComparable t1) {
        return s.equals(t1.s);
    }

}

After making that change, your main would pass compilation and print 1.

That said, I'm not sure how much sense it makes for a method that tests of equality to be called isBiggerThan.

Upvotes: 1

jarvo69
jarvo69

Reputation: 8339

Your comparator interface is expecting a String value and not String array. While when calling that method, you are passing StringComparable array so its showing the error

Upvotes: 0

Related Questions