public class IterableComparator extends Object implements Comparator<Iterable>
This comparator compares two Iterable
s by comparing their items.
Iterables are compared on the first items, then on the second items, etc. If
Iterable A is larger than Iterable B, but all the items of B compared 0
(equal) with the corresponding items in A, then the comparator will sort the
smaller Iterable before the bigger Iterable.
Obviously this comparator would not give predictable results for
Iterable
s which don't have a predictable iteration order, like
HashSet
, but should work well with List
s,
LinkedHashSet
, etc.
The two Iterables don't need to contain items of the same classes, as long as
their element's Comparable.compareTo(Object)
allow comparison. (For
instance a Iterable<String> would not compare against
Iterable<Integer>). As according to
Comparator.compare(Object, Object)
a ClassCastException
would
be thrown in such cases.
Items in the Iterable are compared using NullSafeComparator
, so this
comparator will handle Iterables containing null
items
predictably. This comparator also allows null
in the same way,
so comparison of a Iterable
and null
will sort
null
before the Iterable.
If an item in the Iterable is a Iterable
which is not itself
Comparable
, and it is being compared with a different Iterable, it
will be recursively compared with the same instance of
IterableComparator
. However, if such Iterable
s are compared
with other Comparable
s a ClassCastException
would be thrown.
If an item in a Iterable is not Comparable
null
and
could not be recursively compared, a ClassCastException
will be
thrown, as according to Comparator.compare(Object, Object)
Constructor and Description |
---|
IterableComparator() |
Modifier and Type | Method and Description |
---|---|
int |
compare(Iterable a,
Iterable b) |
protected int |
compareItems(Object a,
Object b) |
protected Integer |
compareNulls(Object a,
Object b) |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
equals
Copyright © 2013 myGrid. All rights reserved.