Search code examples
javagenericsmismatch

Bound Mismatch Error: The type is not a valid substitute


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

Solution

  • 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;
        }
    
    }