• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic collection and Comparable interface

 
Pawel Jaworowski
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,
I have big problem (for me) with I have fought for 2 days and i cant figure it.

I have to write collection which stores string or int (so i decided to use generic type). Function Add - add one item, Removing - remove first element from list, method show - print all list. My collection (called bufor) looks like this:




It works fine, but problme is that i have to implement Comparable interface and sort elements in list. I have no idea how to do it. I saw a lot of tutorials and "how do it", but it seems that there is no examples which could fit to my problem, or even help me a little bit. Problem is with generic type, second problem is that i have no idea how use method compareTo.

Any ideas, or any help?

Thanks a lot,
Ujemny
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A very warm welcome to the ranch Ujemny.


 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Jaworowski wrote:Hello everyone,
I have big problem (for me) with I have fought for 2 days and i cant figure it.

I have to write collection which stores string or int (so i decided to use generic type). Function Add - add one item, Removing - remove first element from list, method show - print all list. My collection (called bufor) looks like this:




It works fine, but problem is that i have to implement Comparable interface and sort elements in list. I have o idea how to do it. I saw a lot of tutorials and "how do it", but it seems that there is no examples which could fit to my problem, or even help me a little bit. Problem is with generic type, second problem is that i have no idea how use method compareTo().

Any ideas, or any help?

Thanks a lot,
Ujemny



If you want your class' elements to be sorted using java.lang.Comparable, why are you writing <T extends Comparable<T>>? Your class should implement the Comparable interface. I don't think you need to use a parametrized type to achieve that. Do you know whatmeans?

Read this and be specific as to what part of the implementation of compareTo(T) it is that you do not understand. It is simply used to compare 2 mutually comparable objects in your collection. What is your doubt? Could you be more specific?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should be Foo<T extends Comparable<? super T>> anyway.
 
Winston Gutkowski
Bartender
Pie
Posts: 10509
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Jaworowski wrote:I have to write collection which stores string or int (so i decided to use generic type).

Which is fine, but you need to remember that a particular List can only hold ONE type of element - that is, either ALL elements are Strings or they're ALL Integers. Mixing and matching is generally a very bad idea.

Also, you can avoid implementing Comparable by using the Collections.sort() method that takes a Comparator (java.lang.Comparator).

Winston
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:It should be Foo<T extends Comparable<? super T>> anyway.


Why to do this at all Campbell? Why to use parametrized types at all? Why not simply implement Comparable<T>?

What does mean?

 
Winston Gutkowski
Bartender
Pie
Posts: 10509
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote:Why to do this at all Campbell? Why to use parametrized types at all? Why not simply implement Comparable<T>?

Because, in the same way that some class hierarchies want subclasses to be able to be mutually compared with equals(), some want their subclasses to be mutually Comparable; and Comparable<? super T> means "Comparable with any object in the same hierarchy".

Winston
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if we assume T to be, say, Integer type, then what that means is that whenever we create an object of Foo<Integer> type, it will be mutually comparable with <Number> object and <Object> object. Is that correct Winston?
 
Winston Gutkowski
Bartender
Pie
Posts: 10509
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote:So if we assume T to be, say, Integer type, then what that means is that whenever we create an object of Foo<Integer> type, it will be mutually comparable with <Number> object and <Object> object. Is that correct Winston?

No. It will depend entirely on how the type you substitute for T is defined, and Integer implements Comparable<Integer>. The point is that it will STILL work if Integer had been defined as Comparable<Number>, and that's what you're trying to achieve with generics: maximum flexibility.

Remember: you have no idea what might be substituted for T, so when adding restrictions, you want to make them as inclusive as possible.

Winston
 
Campbell Ritchie
Sheriff
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you write comparable<T> it means the compareTo() method is written in the same class. As previously stated, if you use Foo<T extends Comparable<? super T>> the compareTo method can be in any supertype. Remember that for the purposes of generics, every type is a supertype of itself.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic