CodesJava

Easy learning with example program codes

what is the difference between comparable and comparator interfaces?


Comparable interface:

Comparable interface is defined in java.lang package. It has only one method namedcompareTo(Object o).Itis used to implement natural ordering of collection elements. String and wrapper classes implements Comparable interface.

 

Comparator interface:

Comparator interface is defined in java.util package. It has two methods named compare(Object obj1,Object obj2) and equals(Object element).

 

Comparable Comparator
It provides only one sort of sequence. It provides multiple sort of sequences.
It provides one method named compareTo(). It provides one method named compare().
It is found in java.lang package. it is found in java.util package.
If we implement Comparable interface, actual class is modified. Actual class is not modified.

 

Java interview questions on collections

Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki