Bound Mismatch Error: The type is not a valid substitute

15k views Asked by At

Bound Mismatch Error: The Type is not a valid substitute

Task: I want to store "Song class object" in ImmutableSet through PlayList class.

I have three classes.

public class ImmutableBinaryTree< T extends Comparable<T>> implements ImmutableSet<T> { }

public class Song { }


public class PlayList<T extends Comparable<T>> {
     private Song songs;  
     ImmutableSet<Song> immutableSet; // Bound Mismatch error occurring here
1

There are 1 answers

1
Kevin Bowersox On BEST ANSWER

If you are adding a Song to ImmutableBinaryTree song must satisfy the bounds of the type parameter on ImmutableBinaryTree, meaning it must implement the Comparable interface since the type parameter specifies the bounds T extends Comparable<T>.

public class Song implements Comparable<Song>{

    @Override
    public int compareTo(Song arg0) {
        //provide implementation here
        //See effective java for appropriate implementation conditions
        return 0;
    }

}