Search code examples
javacollectionscannot-find-symbol

Cannot find Collections.sort despite Comparable being implemented


I read through other questions here and found that when the compiler throws a Cannot find symbol for Collections.sort(List<T> list) the problem was most often either...

  1. It was not passed a List<T>
  2. List<T> doesn't implement Comparable
  3. Importation of java.util.Collection forgotten

I've done all of those things so I suspect that my implementation is somehow wrong. According to this stack overflow entry and the manual my implementation should be legal so I'm out of ideas. Do the rules change if sort is being passed a List<Item>?

Comparable Implementation

 public abstract class Item implements Comparable<Item>
  9 {
 ...//Fields and constructor omitted
 25     @Override
 26     public int compareTo(Item i)
 27     {
 28 //      String title = this.title; DEBUG FLAG: delete maybe?
 29         return this.title.compareTo(i.title); //Returns a negative value if title < i.title, implements alphabetical order by title
 30     }

Call in Library.java (Assumes a properly built TreeMap of LinkedList)

 public Collection<Item> itemsForKeyword(String keyword)
 25     {
 26         List<Item> list;
 27         if(keywordDbase.get(keyword) == null) //There is no mapping at specified keyword
 28         {
 29             list = null;
 30         }
 31         else if(keywordDbase.get(keyword).isEmpty()) //There is a mapping but it is empty
 32             {
 33                 list = null;
 34             }
 35             else //There is a list that has at least one item in it
 36             {
 37                 list = keywordDbase.get(keyword); //stores a reference to the LinkedList in list
 38             }
 39
 40         Collections.sort(list); //DEBUG FLAG: Calling sort may be unnecessary 
 41
 42         return list; here
 43     }

Error

library/Library.java:40: error: cannot find symbol
                Collections.sort(list);
                ^

Solution

  • java.util.Collection differs from java.util.Collections. Add the following import statement to your code:

    import java.util.Collections;`
    

    java.util.Collection is

    the root interface in the collection hierarchy. A collection represents a group of objects, known as its elements. Some collections allow duplicate elements and others do not. Some are ordered and others unordered. The JDK does not provide any direct implementations of this interface: it provides implementations of more specific subinterfaces like Set and List. This interface is typically used to pass collections around and manipulate them where maximum generality is desired.

    On the other hand there is a class java.util.Collections which

    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.

    They are different but deal around the same topic. You just made an unfortunate typo.