[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

difference between comparator and comparable interface

Posted on 2012-09-11
4
Medium Priority
?
365 Views
Last Modified: 2012-09-16
i am preparing for an inerview  I want to brush up my skills

could you explain with comparator and comparable inerface

what is he use of hashcode in hashmap
0
Comment
  • 2
4 Comments
 
LVL 28

Accepted Solution

by:
dpearson earned 2000 total points
ID: 38389630
Comparable is an interface and it means that two things can be compared.  E.g. Integer supports comparable because two numbers can be compared.

A comparator on the other hand is an object.  It's a thing which is used to make a comparison.  E.g. I could write a Comparator for cars.  It would give me a way to define when one car is better than another car.

You typically implement comparable (the first one) to define a default ordering for objects (e.g. numbers that increase).  You use a comparator (the second one) when there are a range of different ways to sort thing - e.g. you might have a price comparator for cars, a quality comparator, a reliability comparator etc.  

I hope that helps,

Doug
0
 
LVL 9

Author Comment

by:Manikandan Thiagarajan
ID: 38389784
comparable means compare with in the object

comparator means  compare with in two objects

is it correct
0
 
LVL 9

Author Comment

by:Manikandan Thiagarajan
ID: 38389801
give me a piece of code to understand
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month20 days, 4 hours left to enroll

873 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