Collection interfaces (Was: Creating a byte[] of long size)
On 7/9/2010 12:50 PM, Patricia Shanahan wrote:
On 7/9/2010 12:45 PM, Tom Anderson wrote:
On Thu, 8 Jul 2010, Eric Sosman wrote:
Or, you could have BigList implement List but "lie" in its .size()
method, in somewhat the same way TreeSet "lies" about the Set contract.
How does TreeSet lie about the Set contract?
The case I'm aware of involves a TreeSet with a Comparator, that is not
consistent with the .equals methods of the TreeSet elements. The TreeSet
always goes by the Comparator results. That means the TreeSet could
contain elements a and b such that a.equals(b).
Patricia
It is my opinion that (given perfect hindsight), the Collections API
should have included several interfaces for comparing items, not just one.
interface Ordering<T> {
enum Order {
LESSER, EQUAL, GREATER
}
Order compare(T left, T right);
}
interface Hasher<T> {
long hash(T value);
}
interface Equivalence<T> {
boolean equal(T left, T right);
}
Then, all the appropriate Collection code could use those interfaces.
There should also be the obvious default implementations.
Not to mention that iterators should have separate methods for advancing
and retrieving, and options for non-fail-fast for certain kinds of
collections (linked lists shouldn't invalidate any iterator unless the
item itself is removed).
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>