3

In the code below Comparable interface is used to ensure x and y should be of same reference type but as V is extending T so V should be of the same type as T or subclass of T then what is the point of using Comparable interface. Also if I am not using Comparable interface then the last call to isIf method is getting compile despite x and y are of different types. Can anyone explain the use Comparable interface regarding this program?

public class Generics_13 {

    static <T extends Comparable<T>, V extends T> boolean isIn(T x, V[] y) {
        for(int i = 0;i < y.length;i++) 
            if(x.equals(y[i])) return true;

        return false;
    }

    public static void main(String[] args) {
        Integer nums[] = {10, 20, 30};

        if(isIn(10, nums))
            System.out.println("10 belongs to the array");
        if(!isIn(60, nums))
            System.out.println("70 doesnt belong to the array");

        String arr[] = {"Neeraj", "Parth", "Ritum"};
        if(!isIn("abc", arr))
            System.out.println("abc doesnt belongs to the array");

        /*if(isIn("String", nums))      //  illegal
            System.out.println("This wont compile");
        */
    }
}
2
  • Object.equals doesn't require an Comparable so why ? What do you want to do with T ? Also, you could use Arrays.binarySearch instead. Commented Dec 29, 2017 at 11:23
  • Place the brackets right behind the data types, instead of behind the variable names, by convention. Commented Dec 29, 2017 at 16:24

2 Answers 2

4

The current use of generics doesn't really makes sense, because no method from Comparable is ever used, which means that you could simply remove the extends declaration.

Also the Type V is also not used, as you can simply replace it by T and not break your logic. so the final result would look like this:

public class Generics_13 {
    static <T> boolean isIn(T x, T[] y) {
        for(int i = 0;i < y.length;i++) 
           if(x.equals(y[i])) return true;

        return false;
    }

    // main() etc follow here
}

But now that we have the Stream API in java-8 you can use the following snippet to achieve the same thing:

static <T> boolean isIn(T x, T[] y) {
    return Arrays.stream(y).anyMatch(i -> i.equals(x));
}
Sign up to request clarification or add additional context in comments.

1 Comment

I was going through a Java book and I was stuck at this program. Thanks, for answering though.
3

The comparable here in your scenario is optional.

T extends Comparable<T>

What this means is that whatever value you are passing should implement the interface comparable. which basically means that the type parameter can be compared with other instances of the same type.

Now you might be wondering that since you are passing primitive data types, then how does the code not throw an error? The reason for this is that the primitives are autoboxed to wrapper objects which implement Comparable. So your ints become Integer and String is already an object both of which implement Comparable.

PS : your code will also work for objects provided that the class implements comparable

9 Comments

I am aware of the autoboxing happening in this program. What I wanted to know was that as V extends T here so y should be of the same datatype as T or subclass of T but I am not getting any error in the last call of isIn method where y is Integer type and x is String type.
they are both Strings in your example. you are referring to this right if(!isIn("abc", arr)) ?
No, I`m talking about the last if structure which I wrote as comments.
are you sure you are not getting any error? that is highly unlikely. With that line the program won't compile.
If I'm not using Comparable interface then I`m not getting any errors.
|

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.