Infolinks In Text Ads

Nov 23, 2010

Comparator Interface

Comparator Interface -
    Comparator Interface is very essential in Java. If you have a group of some objects in a collection. And      you want to maintain order within these objects. Then you will need to implement Comparator Interface.

    It means Comparator Interface is used for managing the order within elements of any collection.

    Sorting order can be ascending order or descending order.

    There is another option for maintaining the shorting order. You can also short a collection through Comparable Interface. But Comparator interface has more generic and gives the facility that you can implement comparator interface in any class and compare any object.

    In the Comparator interface, compare method is exist. Signature of this method is :

public int compare(Object obj, Object obj1)

So you can compare two object which can be related to implementation class or can not.

Return type of compare method is int. It can return negative, positive or zero value.

Negative value indicates that obj is lower than obj1.
Positive value indicates that obj is greater than obj1.
Zero value indicates that obj is equals to obj1.

No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...