mono-thread concurrent modification

C

Chris Uppal

Thomas said:
That's not "wrong". It might give you poor performance in unusual cases,
but so might many things.

What is /wrong/ IMO is not calling get(int) on a LinkedList, but that
LinkedList advertises itself as /suitable/ for (routinely) calling get(int). I
see no reason why LinkedList shouldn't have a get(int) method -- although a
better name might be searchForItemAtIndex(int) -- but that doesn't mean that it
should present itself as interchangable (in this respect) with an ArrayList.

Put it this way, are there any circumstances where it is appropriate to use
get(int) on a LinkedList without knowing that it /is/ a LinkedList ?

The contract implied by an interface doesn't stop at the subtype
relationship -- it is not enough simply to provide methods with the expected
signatures, they should also behave as expected.

public class XList<E>
implements List<E>
{
//... routine stuff

E get(int i)
{
System.exit(i);
}

//... more routine stuff
}

In what useful sense is that a List over and above being a Collection ?

-- chris
 
C

Chris Uppal

Thomas said:
There is no must about it. Indeed java.util.Collections takes a smarter,
adaptive approach.

To my mind, the use of the RandomAccess marker in Collections is better
characterised as indefensible switch-on-type than good programming. Same goes
for java.util.AbstractList.subList(int, int) -- which is the /only/ other place
where that marker interface is used[*] in the entire 1.5 JRE.

-- chris

([*] as the target of an instanceof or checkcast bytecode)
 
M

Mike Schilling

Chris Uppal said:
Thomas said:
There is no must about it. Indeed java.util.Collections takes a smarter,
adaptive approach.

To my mind, the use of the RandomAccess marker in Collections is better
characterised as indefensible switch-on-type than good programming. Same
goes
for java.util.AbstractList.subList(int, int) -- which is the /only/ other
place
where that marker interface is used[*] in the entire 1.5 JRE.

I presume that RandomAccess is new for 1.5; I was unaware of it. It makes
much less sense as a marker interface than as the place where get() etc. are
defined.
 
M

Mike Schilling

Thomas Hawtin said:
It should be. Else you've done it wrong.


I'd be much more happy going into production with something that wasn't
heavily optimised than something that stopped working.

Normal unit testing will find the exception, but not the performance
problem.
 
J

John C. Bollinger

Thomas said:
Most uses of Lists are random access,

The vast majority of *my* uses of Lists are serial access. It is quite
rare that I both want some element in the middle of a List and also know
its index. The most common exception is sorting, but I don't do that
often. On the other hand, ArrayList is the List implementation I
usually use, whether I anticipate random access or not.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top