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)`
1

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

but that is not what i want to do, i want the comparable interface to with string not with stringComparable
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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.