What is difference between comparator and comparable in Java?

Category: technology and computing programming languages
4.7/5 (230 Views . 13 Votes)
Logically Comparator in java compares two objects while Comparable interface compares “this” reference with the object specified. Comparable interface is used to provide the natural sorting of objects and we can use it to provide sorting based on single logic.



Keeping this in view, what is the main difference between comparable and comparator in Java?

Comparable v/s Comparator in Java Comparable interface is used to sort the objects with natural ordering. Comparator in Java is used to sort attributes of different objects. Comparable interface compares “this” reference with the object specified. Comparator in Java compares two different class objects provided.

Also Know, what is difference between compare and compareTo in Java? compareTo() is called on one object, to compare it to another object. compare() is called on some object to compare two other objects. The difference is where the logic that does actual comparison is defined. The relationship of the object having this method and its collaborators is different.

Hereof, what is comparable in Java?

Java Comparable interface is used to order the objects of the user-defined class. This interface is found in java. lang package and contains only one method named compareTo(Object). It provides a single sorting sequence only, i.e., you can sort the elements on the basis of single data member only.

How does a TreeMap work?

TreeMap in Java. The TreeMap is used to implement Map interface and NavigableMap along with the Abstract Class. Also, all its elements store in the TreeMap are sorted by key. TreeMap performs sorting in natural order on its key, it also allows you to use Comparator for custom sorting implementation.

29 Related Question Answers Found

How do you use compareTo?

The Java String compareTo() method is used for comparing two strings lexicographically. Each character of both the strings is converted into a Unicode value for comparison. If both the strings are equal then this method returns 0 else it returns positive or negative value.

Is comparator an interface?

Method 2: Using comparator interface- Comparator interface is used to order the objects of user-defined class. This interface is present in java. util package and contains 2 methods compare(Object obj1, Object obj2) and equals(Object element). Using comparator, we can sort the elements based on data members.

Can we sort HashMap in Java?

HashMap is not meant to keep entries in sorted order, but if you have to sort HashMap based upon keys or values, you can do that in Java. Sorting HashMap on keys is quite easy, all you need to do is to create a TreeMap by copying entries from HashMap. This is similar of how you sort an ArrayList in Java.

What does compareTo return in Java?

The compareTo method is used when we need to determine the order of Strings lexicographically. It compares char values similar to the equals method. The compareTo method returns a negative integer if the first String object precedes the second string. It returns zero if the 2 strings being compared are equal.

Why comparator is used in Java?


Comparator can be used to compare instances of different classes. Comparable is implemented by class which need to define a natural ordering for its objects. Like String implements Comparable. In case one wants a different sorting order then he can implement comparator and define its own way of comparing two instances.

How do you compare two different objects?

If the two objects have the same values, equals() will return true . In the second comparison, equals() checks to see whether the passed object is null, or if it's typed as a different class. If it's a different class then the objects are not equal. Finally, equals() compares the objects' fields.

What does the hashCode () method?

Java hashCode()
Java Object hashCode() is a native method and returns the integer hash code value of the object. If two objects are equal according to equals() method, then their hash code must be same. If two objects are unequal according to equals() method, their hash code are not required to be different.

Can you extend an interface?

Extending Multiple Interfaces
A Java class can only extend one parent class. Multiple inheritance is not allowed. Interfaces are not classes, however, and an interface can extend more than one parent interface. The extends keyword is used once, and the parent interfaces are declared in a comma-separated list.

What is natural sorting in Java?

Java allows you to sort your object in natural order by implementing Comparable interface. Comparable is used to provide natural order of sorting to objects e.g. numeric order is natural order for numbers, alphabetic order is natural order for String and chronological order is natural for dates.

What is an interface?


In computing, an interface is a shared boundary across which two or more separate components of a computer system exchange information. The exchange can be between software, computer hardware, peripheral devices, humans, and combinations of these.

What is serialization in Java?

Serialization is a mechanism of converting the state of an object into a byte stream. Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory. To make a Java object serializable we implement the java. io. Serializable interface.

What is singleton class in Java?

Singleton Class in Java. In object-oriented programming, a singleton class is a class that can have only one object (an instance of the class) at a time. To design a singleton class: Make constructor as private. Write a static method that has return type object of this singleton class.

Why is string immutable in Java?

The string is Immutable in Java because String objects are cached in String pool. Another reason of why String class is immutable could die due to HashMap. Since Strings are very popular as HashMap key, it's important for them to be immutable so that they can retrieve the value object which was stored in HashMap.

What is the purpose of generics in Java?

Why Use Generics? In a nutshell, generics enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods. Much like the more familiar formal parameters used in method declarations, type parameters provide a way for you to re-use the same code with different inputs.

What is lambda expression in Java?


Java lambda expressions are Java's first step into functional programming. A Java lambda expression is thus a function which can be created without belonging to any class. Java lambda expressions are commonly used to implement simple event listeners / callbacks, or in functional programming with the Java Streams API.

How does HashMap work in Java?

HashMap in Java works on hashing principle. It is a data structure which allows us to store object and retrieve it in constant time O(1) provided we know the key. HashMap internally stores mapping in the form of Map. Entry object which contains both key and value object.

How do you compare two variables in Java?

The right way of comparing String in Java is to either use equals(), equalsIgnoreCase(), or compareTo() method. You should use equals() method to check if two String contains exactly same characters in same order. It returns true if two String are equal or false if unequal.