difference between comparator and comparable interface

Posted on 2012-09-11
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
Question by:stmani2005
    LVL 26

    Accepted Solution

    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,

    LVL 8

    Author Comment

    comparable means compare with in the object

    comparator means  compare with in two objects

    is it correct
    LVL 8

    Author Comment

    give me a piece of code to understand
    LVL 37

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now