Discussion:
UnsupportedOperationException
zappini-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org [seajug]
2014-07-03 00:01:42 UTC
Permalink
I was surprised this doesn't work.


List<String> argh = Arrays.asList( "ABC","XYZ" );

argh.clear();


Then I had to read the javadoc. Saays "proxy for original list", not "the result is immutable".


Further, an immutable list shouldn't have a clear() method.
Dennis Sosnoski dms-WAiJhE/vqclWk0Htik3J/w@public.gmane.org [seajug]
2014-07-03 00:20:10 UTC
Permalink
One of the many questionable design decisions for the Java collections
classes. It would have made more sense to have an basic List interface
that just defined the non-mutating methods, then a MutableList interface
that extended List. And I would have added separate RandomAccessList and
MutableRandomAccessList interfaces which guarantee constant time access
to items in the list (vs. linked lists with O(n) access time).

- Dennis
Post by zappini-***@public.gmane.org [seajug]
I was surprised this doesn't work.
List<String> argh = Arrays.asList( "ABC","XYZ" );
argh.clear();
Then I had to read the javadoc. Saays "proxy for original list", not
"the result is immutable".
Further, an immutable list shouldn't have a clear() method.
George Smith litesoft-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org [seajug]
2014-07-03 14:10:52 UTC
Permalink
I completely concur w/ Dennis.

Since the GOF (Gang Of Four) "Design Patterns" book was published in 94/95,
and the Java Collections were released in December of 98, it has been a
major frustration of mine that the the Java Collections did not implement
the GOF Iterator!

The GOF Iterator, with the following methods:

- First()
- Next()
- IsDone()
- CurrentItem()

provides the following things that the Java Iterator doesn't:

1. reusable (can traverse the backing collection as many times as you
need),
2. a Read-Only View,
3. repeated access to the Current Item (w/o needing a local variable).

These features, in combination, meant that except for Random Access, a GOF
Iterator WAS a reasonable accessor return type for read-only access to an
underlying collection.

George
Post by Dennis Sosnoski dms-WAiJhE/vqclWk0Htik3J/***@public.gmane.org [seajug]
One of the many questionable design decisions for the Java collections
classes. It would have made more sense to have an basic List interface that
just defined the non-mutating methods, then a MutableList interface that
extended List. And I would have added separate RandomAccessList and
MutableRandomAccessList interfaces which guarantee constant time access to
items in the list (vs. linked lists with O(n) access time).
- Dennis
I was surprised this doesn't work.
List<String> argh = Arrays.asList( "ABC","XYZ" );
argh.clear();
Then I had to read the javadoc. Saays "proxy for original list", not
"the result is immutable".
Further, an immutable list shouldn't have a clear() method.
--
"And the users exclaimed with a laugh and a taunt: It's just what we
asked for but not what we want." -- Unknown
Loading...