문제

I came across this old code in the process of adding java generics and I do not understand what is going on here and what needs to be changed.

static void sortByDate( List list)   throws Exception
{  
    class MyComparator implements Comparator {

        public int compare(Object o1, Object o2) {

            RQEntry  o11 = (RQEntry) o1;
            RQEntry  o22 = (RQEntry) o2;

            int cc = ((String)o11.getHandledDate() ).compareTo(o22.getHandledDate() );

            return (cc < 0 ? -1 : cc > 0 ? 1 : 0);
        }
    };

    Collections.sort(list, new MyComparator());
}

Question 1: Could someone explain how this method performs comparison?

Question 2: What generic type parameters should be passed to comply with Java 5 standards?

Question 3: How is compare method called just by creating a new instance of MyComparator?

Update:

Just found this link which finally helped me understand this code: http://www.digizol.com/2008/07/java-sorting-comparator-vs-comparable.html

도움이 되었습니까?

해결책

If you have a List<RQEntry> you can sort it now using this comparator.
The RQEntry objects will be compared based on their handled dates.

This is what this call here is doing.

Collections.sort(list, new MyComparator());

Behind the scenes, when the sort algorithm needs to compare
two RQEntry objects, it will call the compare method of your
comparator. That's all.

다른 팁

This code allows you to sort a List by date. It makes an assumption that the List is actually a List<RQEntry>.

Java allows you to call Collections.sort(list, new SomeComparator()). The comparator you specify in the second argument contains the code that decides how to order items in the list.

The actual comparison interface is that you need to implement int compare(Object o1, Object o2). According to the specification, you need to return 0 if the two objects are the same, a negative value if the first item is ordered before the second item, and a positive value if the second item is ordered before the first.

As an example, if you had a list containing [37, 19] then calling compare(37, 19) would return 1 (assuming you want to sort a list of integers in ascending order).

Answer 1: It is doing a string comparison between whatever is returned from the getHandleDate method. I have no idea why it is doing:

return (cc < 0 ? -1 : cc > 0 ? 1 : 0);

since cc should by all rights be -1, 0 or 1. The code could just as easily do:

return ((String)o11.getHandledDate()).compareTo(o22.getHandledDate());

Answer 2: You could use generics to simplify as follows:

static void sortByDate(List<RQEntry> list)   throws Exception
{  
    Collections.sort(list, new Comparator<RQEntry> {
        public int compare(RQEntry o1, RQEntry o2) {
            return ((String)o1.getHandledDate()).compareTo(o2.getHandledDate());
        }
    });
}
Question 1: Could someone explain how this method performs comparison?

For each object the getHandledDate() method is called and the return values (obviously Strings) are compared (default comparison is lexicograhical ordering.

The tricky part is understanding the return value of the String.compareTo method used. It is defined in the Comparable interface.

Question 2: What generic type parameters should be passed to comply with Java 5 standards?

The generic type should obviously be RQEntry. -> class MyComparator implements Comparator<RQEntry>.

See also:

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top