Hi,
1. java.util.Comparator is an interface, so in all static methods
(stringComparator(), integerComparator() and dateComparator) there is
anonymous class created and method compare from java.util.Comparator is
overridden.First return statement returns an instance of anonymous
Comparator class, second return statement returns result from overridden
compare method.
2. Method Collections.sort(List, Comparator) uses compare method, thats why
an comparator instance is passed as argument.

Ad. 1 If anonymous class is unfamiliar, this do the same:
public class MyComparators {

    // String Comparator object
    public static Comparator stringComparator() {
        return new StringComparator();
    }

    // Integer Comparator object
    public static Comparator integerComparator() {
        return new IntegerComparator();
    }

    class StringComparator implements Comparator {
        public int compare(Object o1, Object o2) {
                //implementation, which compares strings
                return result;
           }
    }

    class IntegerComparator implements Comparator {
        public int compare(Object o1, Object o2) {
                //implementation, which compares integers
                return result;
           }
    }
}

Regards,
P.Skarzynski





2014/1/7 Sergio G Barreros <[email protected]>

>
> Ok,
> as the subject of my first post states, This is from Lab 1016(5.2) Sort a
> list using Comparator. In that lesson, there is a class named
> MyCompartors.class, that class contains a method called stringComparator()
> .
> 1) The first line of that method is a return statement and there is
> another one at the bottom of the same stringComparator() method. Why does
> it have multiple return statements?
> 2) The Main.java file calls on the MyComparators.stringComparator(); method,
> but never uses the compare(Object o1, Object o2) method which i s the
> only method there. Please provide explanation on this.
>
> Code should be available on the Collections labs, but I am attaching them
> here anyway.
>
> On Monday, January 6, 2014 2:15:36 PM UTC-5, Deepak A L wrote:
>
>> hi.
>>     how does it work...confused.
>>
>> attach the working example team of jpassion with correct explanation....
>>
>> Regards,
>> Deepak
>> On Jan 6, 2014 12:14 AM, "Sergio G Barreros" <[email protected]> wrote:
>>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "JPassion.com: Java Programming" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> Visit this group at http://groups.google.com/group/jpassion_java.
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
You received this message because you are subscribed to the Google Groups 
"JPassion.com: Java Programming" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
Visit this group at http://groups.google.com/group/jpassion_java.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to