Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

interface

Posted on 2005-04-17
7
Medium Priority
?
198 Views
Last Modified: 2011-09-20
when do we use comparator and comparable?.

Your help is kindly appreciated.

Regards

Eugene
0
Comment
Question by:eugene007
  • 5
  • 2
7 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 13801078
When objects need to be sorted. Implementing Comparable gives the ability for the Collections class to do a default sort on the object in question. A Comparator can provide an non-existent implementation or override one
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13801084
In the case of your previous question, Department itself implemented Comparable, meaning that Collections could do a default sort. If you used a Comparator, you could override that implementation and sort by some other criteria
0
 

Author Comment

by:eugene007
ID: 13801830
why do we use interface.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 86

Expert Comment

by:CEHJ
ID: 13801841
An interface is a way of specifying a behavioural contract between a 'server' and a 'client' without specifying how the behaviour is implemented
0
 

Author Comment

by:eugene007
ID: 13801876
so for comparable:

Collections.sort(list); // natural order

provided the compareTo() method is not overriden, otherwise the sorting will be based on criteria.


Collection.sort(list, new Comp())

The compare() method has been overriden. The sorting is base on criteria.



0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13801925
That's is basically, yes
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13802046
:-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month14 days, 8 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question