File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I m still not understanding COMPARABLE Interface

 
saurabhthard aggarwal
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please give me SIMPLE ,COMPLETE example of Comparable interface.Also its main features.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason why you're not getting good answers is because your question is very vague and general. Can you explain in more detail what exactly you don't understand about the Comparable interface?

Did you read the API documentation? What part of it did you not understand? Have a look at The Collections Framework, which explains how collections work, including the Comparable interface.

Try writing some code yourself to find out how it works. If your code doesn't work, post it here and ask questions about it.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
 
K. Tsang
Bartender
Pie
Posts: 3222
12
Android Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a more common problem would be whether to use the java.lang.Comparable interface or the java.util.Comparator interface. The goal of both of these interface is what?? sort object right?

Comparable uses the "compareTo(Object o)" method
Comparator uses the "compare(Object o1, Object o2)" method

If you implement either of these interfaces you normally override the equals and hashCode methods and good idea to override toString too.

Both of the above methods uses the equals method to compare in order to sort that object.

Talking about sort, using Collection.sort() only sort strings and primitive collections. More complicated objects would not be appropriate so using either of these is the way to go.

At the end of the day, use Comparable to do simple sorting, otherwise use Comparator to have more control.

Best to is to test out both by sorting the same object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic