Re: Generics
On Mar 11, 8:59 am, Todd <todd.heident...@lmco.com> wrote:
On Mar 10, 5:13 pm, Lew <l...@lewscanon.com> wrote:
Todd wrote:
import java.util.StringTokenizer;
import java.util.Vector;
Don't use java.util.Vector. Use one of the List implementations that =
replaced
it in 1998, such as ArrayList.
public <T> T getValue()
{
@SuppressWarnings( "unchecked" ) // TODO check cast=
and don't
suppress
Class <T> type = dataType.getClassType();
The need for an "unchecked" suppression is an indicator that you don't h=
ave
the generics quite right. Don't feel bad; with type erasure it isn't =
easy to
get generics exactly right, warning-free.
--
Lew
Please don't quote signatures.
Lew,
Question about ArrayList vs. Vector. I remember reading that one
should only use Vector since it is the only collection _guaranteed_ to
be thread-safe. Is this no longer considered true?
It depends on what you mean by "thread-safe". All of Vector's methods
are declared synchronized, which means that only one method may be in
progress on a single Vector at a time. However, this provides no
guarantees about coherency across multiple method calls, nor
particularly across iteration.
If you need the same promise from a List, you can use
Collections.synchronizedList (backingList) to construct a wrapper
around a List that performs the same synchronization that Vector
does. However, I've rarely found synchronizing on individual lists to
be all that useful, for the reasons I outlined above.
-o