Reputation: 6444
For anyone who might have a question like this, you probably need "Collections.sort", not "Collection.sort", the mistake I made below.
I have defined a class defined as
public class Store implements Serializable, Comparable<Store> { ... }
I have a field in another class defined as:
ArrayList<Store> fStores = new ArrayList<Store>();
I want to sort this collection, so in a method I call:
Collection.sort(fStores);
However, I get the following compilation error:
The method sort(ArrayList<Store>) is undefined for the type Collection
ArrayList implements List, and from the documentation:
public static <T extends Comparable<? super T>> void sort(List<T> list)
So, why do I get the error? I have also tried creating my own descendant of Comparator and passing that to the sort method with no luck.
I'm guessing there's something about "< T extends Comparable< ? super T > >" I'm not understanding... ?
Upvotes: 7
Views: 30807
Reputation: 16990
In case you got here due to a similar error:
The method sort(Comparator<...>) is undefined for the type List<...>
Then you're probably using JDK lower then 1.8. Change it in Java Build Path:
Upvotes: 0
Reputation: 1
Also, if you are using eclipse, double check that the import statement for Collections is the right one!
Upvotes: 0
Reputation: 3054
There are basically 2 things that you need to look at :
Collections
From the Collections
This class consists exclusively of static methods that operate on or return collections. It contains polymorphic algorithms that operate on collections, "wrappers", which return a new collection backed by a specified collection, and a few other odds and ends
So basically if you have to sort or do any such kind of algorithms use this.
Next is :->
Collection
This is an interface that provides the basis of Java's collection framework. It does not include Map and Sorted Map. It represents a group of objects known as its elements and has implementations for concrete implementations. You need to think of this when you want to work with ArrayLists and Maps.
So, bottom line, you have a static algorithm to run which is present in Collections. So, use Collections.sort
Upvotes: 7
Reputation: 5452
Your issue is that you are calling Collection.sort()
, not Collections.sort()
These are two separate classes:
Collection is an interface while Collections is a utility class.
Upvotes: 1
Reputation: 12766
It's Collections.sort() with an s.
import java.util.Collections
and you should be fine.
Upvotes: 1
Reputation: 183446
You need to write Collections
instead of Collection
. They're related, but different. :-)
Upvotes: 4