top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the Comparable interface in JAVA?

+1 vote
328 views
What is the Comparable interface in JAVA?
posted Mar 4, 2016 by Ritika Sharma

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes

Comparable interface

Comparable interface is used to order the objects of user-defined class.This interface is found in java.lang package and contains only one method named compareTo(Object).It provide only single sorting sequence i.e. you can sort the elements on based on single datamember only.For instance it may be either rollno,name,age or anything else.

Syntax:

public int compareTo(Object obj): is used to compare the current object with the specified object.

We can sort the elements of:

  1. String objects

  2. Wrapper class objects

  3. User-defined class objects

Collections class provides static methods for sorting the elements of collection.If collection elements are of Set type, we can use TreeSet.But We cannot sort the elements of List.Collections class provides methods for sorting the elements of List type elements.

answer Mar 7, 2016 by Karthick.c
0 votes

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

answer Mar 9, 2016 by Ashish Kumar Khanna
...