1

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)`
AlBlue
  • 23,254
  • 14
  • 71
  • 91
Marco Dinatsoli
  • 10,322
  • 37
  • 139
  • 253
  • refer this: http://stackoverflow.com/questions/17739720/inferred-type-is-not-a-valid-substitute-for-a-comparable-generic-type – Boola Jun 21 '16 at 09:16

3 Answers3

1

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.

Eran
  • 387,369
  • 54
  • 702
  • 768
0

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

jarvo69
  • 7,908
  • 2
  • 18
  • 28
0

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.

AlBlue
  • 23,254
  • 14
  • 71
  • 91
Sergii Bishyr
  • 8,331
  • 6
  • 40
  • 69