To sort the objects of user-defined classes, a comparator interface is utilized. A comparator object can compare two items of the same type.This interface may be found in java.util package and includes two methods: compare(Object obj1,Object obj2) equals (Object element). It supports different sorting sequences, which means you can sort the items based on any […]
August 6, 2022 | Java | No comments
The Comparable interface is used to compare an object of the same class with an instance of that class; it also enables data ordering for user-defined class objects. The class has to implement the java.lang.Comparable interface to compare its instance, it provides the compareTo() method, which accepts an object of that class as an argument. Its sorting algorithm is determined by the type of object used by the interface. If the object type is a string, it is sorted lexicographically. Else, If the object type is a wrapper class object, such as an integer […]
August 5, 2022 | Java | No comments