I'm doing this for my own understanding and I don't understand this question.
Assume there is an interface named comparable with the following definition:
public interface Comparable {
int compareTo(Object obj);
}
Do you think class String implements interface Comparable? Provide a reason for your answer.
I would say no because there is no string anywhere in the class, but I believe I am misunderstanding the question. Can Someone explain this one better for me?
String s implement the Comparable<String> interface. If an object is of a class that implements Comparable , then that object is less than, equal, or greater than any object of that class. compareTo() returns an integer to show which of these three relations hold.
Using Comparable InterfaceOverride the compareTo method in the Pair class. Create an array of Pairs and populate the array. Use the Arrays. sort() function to sort the array.
The Comparable interface defines the compareTo method used to compare objects. If a class implements the Comparable interface, objects created from that class can be sorted using Java's sorting algorithms.
For any class to support sorting, it should implement the Comparable interface and override it's compareTo() method. It returns a negative integer if an object is less than the specified object, returns zero if an object is equal, and returns a positive integer if an object is greater than the specified object.
You should look into the source code or the Java doc of the class String
. It is explicitly implementing the interface Comparable
.
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html
java.lang
Class String
java.lang.Object
java.lang.String
All Implemented Interfaces:
Serializable, CharSequence, Comparable< String >
I'm going to give you other point of view for the question and I'm goint to say No, it doesn't.
Since Java 6, java.lang.String can't implement that interface because that is not compatible with Comparable<String>
and Comparable
is not the same as Comparable<String>
. The diference is that you can't compare an String
with an Object
String implements Comparable.
Why it does so?
So that sorting can be done on strings using compareTo method. For example you want to sort employees based on their name and name is string so you use this as:-
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class Employee implements Comparable {
String name;
public Employee(String name) {
this.name = name;
}
public int compareTo(Object o) {
Employee employeeAnother = (Employee) o;
// natural alphabetical ordering by type
// if equal returns 0, if greater returns +ve int,
// if less returns -ve int
return this.name.compareTo(employeeAnother.name);// String compareTo is
// called here
}
public static void main(String[] args) {
List list = new ArrayList();
list.add(new Employee("Ramesh"));
list.add(new Employee("Deepak"));
list.add(new Employee("Vivek"));
Collections.sort(list); // sorts using compareTo method
for (Iterator iter = list.iterator(); iter.hasNext();) {
Employee employee = (Employee) iter.next();
System.out.println(employee);
}
}
public String toString() {
return name;
}
}
I have not used Generics to keep things simple. You will get few warnings but you can ignore these.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With