Error: Bound mismatch, Collections.sort, interface, compareTo Method

Derekyy

I want to compare a new class called CalendarDate. I made an interface Comparable, and I make the public class CalendarDate implementing Comparable. I also wrote a compareTo(CalendarDate Other) method in the class.

However, when I try Collections.sort(birthday), it says Bound mismatch. (It has details: Bound mismatch: The generic method sort(List) of type Collections is not applicable for the arguments (ArrayList). The inferred type CalendarDate is not a valid substitute for the bounded parameter >)

what is wrong with my code? Thanks.

P.S. There is a RED line under the word sort in CalendarDateTest.java.

Comparable.java

package Chapter10;

public interface Comparable<T> {
    public int compareTo(T other);
}

CalendarDate.java

package Chapter10;

public class CalendarDate implements Comparable<CalendarDate> {
    private int month;
    private int day;

    public CalendarDate(int month, int day) {
        this.month = month;
        this.day = day;
    }

    public int compareTo(CalendarDate other) {
        if (month != other.month) {
            return month - other.month;
        }
        else {
            return day - other.day;
        }
    }

    public String toString() {
        return month + "/" + day;
    }
}

CalendarDateTest.java

package Chapter10;

import java.util.*;

public class CalendarDateTest {
    public static void main (String[] args) {
        ArrayList<CalendarDate> birthday = new ArrayList<CalendarDate>();
        birthday.add(new CalendarDate(1, 23));
        birthday.add(new CalendarDate(5, 18));
        birthday.add(new CalendarDate(12, 17));
        birthday.add(new CalendarDate(2, 29));
        birthday.add(new CalendarDate(8, 6));

        System.out.println("birthdays = " + birthday);
        Collections.sort(birthday);
        System.out.println("birthdays = " + birthday);
    }
}
Sergey Kalinichenko

The problem is that you have re-defined the Comparable<T> interface that Java has already defined. As far as Java is concerned, these are two entirely different, unrelated interfaces, despite having the same name. The Collections.sort method knows about java.lang.Comparable<T>, but it has no idea about your Chapter10.Comparable<T>.

Remove Comparable.java file from your project, recompile, and run; this should fix the problem.

Running demo on ideone.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Collections.sort() not sorting with compareTo Override

Error with compareTo method

CompareTo method error

Why getting a type mismatch when using Collections.Sort method with a List<String?>?

Why use compareTo() from Comparable Interface when overriding the compareTo() method?

Collections.sort() error

adding comparable interface and adding a compareTo() method

compareTo method logic to sort List by multiple variables

Sort objects in Java without 'proper' compareTo method

Why doesn't invalid compareTo cause Collections.sort to crash?

Collections.sort not working after overriding compare and compareTo

Bound method error

Java Generics Bound Mismatch Compilation Error

Bound mismatch for generic method from "Java Effective" example rewritten

Error Message: The method sort(List<T>) in the type Collections is not applicable for the arguments (ArrayList<Date>)

How to sort list with compareTo method of specific order of colors in Java

How compareTo method works here to sort objects one by one

How does the sort() method of the Collection class call the Comparable's compareTo()?

Overriding compareTo method in order to sort objects by String values

Collections.sort generic method signature

No suitable method found for Collections.sort()

Cannot sort the array , index out of bound error

How to sort a DataGrid by a column that's been bound to an interface property in WPF?

Using Collections.sort() method to sort objects alphabetically

Error while sorting using Collections sort

Java generics: Bound mismatch

Bound mismatch with Generic Types

Bound mismatch using DoubleSupplier

Bound mismatch for Collectiondatasource working