Collection Basics
What is Collection
Why Collection ?
Collection API
Collection Hierarchy
What is List
What is ArrayList
What is LinkedList
What is Vector
method of vector class in java
What is Stack
List Advance
ArrayList user defined object
ArrayList internal implementation
LinkedList internal implementation
ArrayList VS LinkedList
ArrayList VS Vector
What is HashSet
What is LinkedHashSet
What is TreeSet
Set Advance
When to Use HashSet
HashSet internal Working
When to use LinkedHashSet
TreeSet internal Working
Set add method
Iterate Collection
Iterate Collection
Advance for loop
What is Iterator
Advantage of Iterator
What is ListIterator
Iterator VS ListLterator
What is Enumeration
Enumeration VS Iterator
What is HashMap
What is LinkedHashMap
What is TreeMap
What is Hashtable
What is WeakHashMap
What is IdentityHashMap
What is Properties file
Map Advance
What is Load-Factor
how to load properties file in java
how to iterate map in java
how to retrieve element of map using keySet
difference between hashmap and hashtable
Map Interview
Internal implementation of HashMap
HashMap Collision
Rehashing Process
Internal working of LinkedHashMap
What is Priority Queue
Concurrent Collections
Concurrent Collections
Concurrentmap in java
Why ConcurrentModificationException
What is ConcurrentHashMap
Internal Implementation of CHM
HashMap VS ConcurrentHashMap
What is CopyOnWriteArrayList
What is CopyOnWriteArraySet
Concurrent Map 2
Arraylist VS CopyOnWriteArrayList
put() VS putIfAbsent()
remove() method
replace() method
Collection Sorting
Comparable VS Comparator
Sort by multiple fields
Legacy class
Arrays class
What is Collections
Collection VS Collections
immutable object

Difference between Comparable and Comparator in java

Difference between Comparable and Comparator in java

Difference between Comparable and Comparator in java is an important interview question for every Java Developer because interviewer want to know your level of expertise in Collection Framework.

 Comparable  Comparator
 Comparable interface provides natural order sorting order by default. Comparator interface provides customized sorting order.
Comparable interface present in java.lang package.  Comparator interface present in java.util package.
Comparable contains only one method compareTo() method.  Comparator contain two method compare() and equals() method.
 All wrapper classes like Integer, Float etc and String class implemented Comparable interface.  Collator and RuleBasedCollator are implemented by Comparator interface.
Comparable should be used when you compare instances of same class. Comparator can be used to compare instances of different classes.
A comparable object is capable of comparing itself with another object. A comparator object is capable of comparing two different objects.
We need to modify class code if we want to change sorting logic in case of Comparable. We no need to modify the existing class we can create another class to change sorting logic.

In short

Implementing Comparable means “We can compare myself with another object.” This is typically useful when there’s a single natural default comparison.

Implementing Comparator means “I can compare two other objects.” This is typically useful when there are multiple ways of comparing two instances of a type – e.g. you could compare people by age, name etc.

Related Posts

  1. Comparable
  2. Comparator

Full-stack web and mobile app development company Contact Us : Skype: indore.webcoder Email: Sales: +91 8085506229