aspose file tools*
The moose likes Java in General and the fly likes Collections.sort and generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Collections.sort and generics" Watch "Collections.sort and generics" New topic
Author

Collections.sort and generics

Maxim Katcharov
Ranch Hand

Joined: Sep 07, 2004
Posts: 113
I think I'm missing something with generics. Here's the code that I'm having trouble with:


class Foo implements Comparable {...

... within another class:

private List<Foo> fooList;

void addFoo(Foo f) {
fooList.add(f);
Collections.sort(fooList); //gives me an error
}


the error:
the generic method sort(List<T> of type Collections is not applicable for the arguments (List<Foo> since the type Foo is not a valid substitute for the bounded parameter <T extends Comparable<? super T>>


could someone explain what's going on?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Look carefully at the message - "the type Foo is not a valid substitute for the bounded parameter <T extends Comparable<? super T>>"
It's not enough that Foo implements Comparable; you need to implement Comparable<Object> or Comparable<Foo>.


"I'm not back." - Bill Harding, Twister
Maxim Katcharov
Ranch Hand

Joined: Sep 07, 2004
Posts: 113
...and it all makes sense. Thanks Jim.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collections.sort and generics
 
Similar Threads
generics
Generics
Generics
generics
Casting Around In Generics