mam plz look into my code .please mark me with the error i my code i have made a new class and that class is implementing the comparable interface.
Unable to Pass test cases
if (s[i].sal > = k) {
System.out.println(s[i].name + " " + s[i].sal);
}
make it >=
instead of >
Thank you .Also mam what is the difference in implementing comparable or comparator interface.
- Comparable interface can be used to provide single way of sorting whereas Comparator interface is used to provide different ways of sorting.
- For using Comparable, Class needs to implement it whereas for using Comparator we don’t need to make any change in the class.
- Comparable interface is in
java.lang
package whereas Comparator interface is present injava.util
package. - We don’t need to make any code changes at client side for using Comparable,
Arrays.sort()
orCollection.sort()
methods automatically uses thecompareTo()
method of the class. For Comparator, client needs to provide the Comparator class to use in compare() method.
go through this article for more info: