0

So I have seen multiple questions addressing similar problems to mine, but I was not able to find one that was exactly like my problem.

I have an ArrayList of Contact objects, and I want to sort them using Collections.sort:

public void sortContactArrayList(){
    ArrayList<Contact> newList = new ArrayList<Contact>();
    Collections.sort(newList);
}

In order to do this, I made Contactimplement Comparable<Contact>. And, my compareTo method looks like this:

@Override
public int compareTo(Contact otherContact) {
    return this.getName().compareTo(otherContact.getName());
}

However, I am receiving an error when I am calling Collections.sort(newList);

The error is:

"Bound mismatch: The generic method sort(List<T>) of type Collections is not applicable for the arguments (ArrayList<Contact>). The inferred type Contact is not a valid substitute for the bounded parameter <T extends Comparable<? super T>>"

Does anyone know what the issue is? Like I said, I have seen this similar problem with a customized list of certain objects like "ContactDatabase<Contact>" or something, but I have never seen this problem with just a certain object itself.

Thank you!

2
  • 3
    That message means that Contact does not implement Comparable<Contact>. Are you sure you've recompiled it and your compiler is looking at the right .class file? Commented Apr 23, 2014 at 2:05
  • It seems right for me. Could you post the code of Contact? Commented Apr 23, 2014 at 2:12

1 Answer 1

1

It should be OK if you implemented Comparable<Contact>.

Here is my quick test code:

Contact.java:

public class Contact implements Comparable<Contact> {

    private String name;

    public Contact(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @Override
    public int compareTo(Contact otherContact) {
        return this.getName().compareTo(otherContact.getName());
    }
}

main method:

public static void main(String[] args) {

    ArrayList<Contact> newList = new ArrayList<Contact>();
    newList.add(new Contact("Midgar"));
    newList.add(new Contact("David"));
    Collections.sort(newList);
    System.out.println(newList);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! It does work, and I have no idea why it didn't before. For some reason, I didn't have to change anything, I just didn't use Eclipse for a day, and went on it today and it worked. I was almost convinced that the code was correct before I posted my question, so I needed someone to verify that I was right before I started blaming Eclipse acting up. Thank you!
@Midgar77 sometimes you can try clean and rebuild project in eclipse, or close/reopen eclipse to make things right.

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.