This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Generic collection and Comparable interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Generic collection and Comparable interface" Watch "Generic collection and Comparable interface" New topic
Author

Generic collection and Comparable interface

Pawel Jaworowski
Greenhorn

Joined: Apr 02, 2013
Posts: 1
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

Joined: Jul 27, 2010
Posts: 1157

A very warm welcome to the ranch Ujemny.



~ Mansukh
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Oct 13, 2005
Posts: 37902
    
  22
It should be Foo<T extends Comparable<? super T>> anyway.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7492
    
  18

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Mar 17, 2011
Posts: 7492
    
  18

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

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Mar 17, 2011
Posts: 7492
    
  18

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

Joined: Oct 13, 2005
Posts: 37902
    
  22
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.
 
Consider Paul's rocket mass heater.
 
subject: Generic collection and Comparable interface
 
Similar Threads
Implementing ArrayList class manually
Trouble with ADT lists
Gnerics Doubt.
Sorting an ArrayList
Iterator Pattern