Fwd: Cleanup patch: Change from Vector/Hashtable to ArrayList/HashMap - Mailing list pgsql-jdbc

From Luis Flores
Subject Fwd: Cleanup patch: Change from Vector/Hashtable to ArrayList/HashMap
Date
Msg-id CA+nXnG8b7wTkb9SM5dX-X9NrEBfHYk_-DCkyoFk_ssnKm2sS9Q@mail.gmail.com
Whole thread Raw
In response to Re: Cleanup patch: Change from Vector/Hashtable to ArrayList/HashMap  (dmp <danap@ttc-cmc.net>)
Responses Re: Fwd: Cleanup patch: Change from Vector/Hashtable to ArrayList/HashMap  (Lew <noone@lewscanon.com>)
List pgsql-jdbc
Some old 1.0 'Collections' (namely Vector and HashTable) where marked obsolete, they won't disappear beacause they are too widely used, but they are very inefficient, so they have better alternatives (ArrayList and HashMap), event if you want an synchronized List or Map you should use Collections.synchronized[Collection|Map|List|Set] and not use the old classes.

However same uses of Vector don't work with newer classes, for example, the method set won't work on ArrayList (ArrayList with size 10 and set at position 20, you get Exception, on Vector it works).

So, excluding the example above, it is a good thing to replace Vector by List/ArrayList and Hashtable by Map/HashMap. 
In some case LinkedList is better than ArrayList.

Luis Flores


On Sun, May 6, 2012 at 8:59 PM, Robin Rosenberg <robin.rosenberg@dewire.com> wrote:
dmp skrev 2012-05-06 20.03:

Mikko Tiihonen wrote:
 > Hi,
 >
 > Here are two small cleanup patches.
 >
 > First one replaces all Vector references with List (usage) and ArrayList
 > (creation).
 > Second replaces all Hashtable references with Map (usage) and HashMap
 > (creation).
 >
 > Reasons:
 > * Theoretically faster since the ArrayList/HashMap are not synchronized
 > * Using interfaces makes changing of List/Map implementations easier at
 > later time
 > * Vector/Hashtable were deprecated already in Java 1.2

My JDK 6 documentation gives no indication that either Vector or Hashtable
as being deprecated. Rather them being retrofitted into the Collectionss
Framework at 1.2. The main difference being that as you said they are
synchronized, which a developer may wish to have for that type of data
structure.

They are not deprecated in the "this might go away" sense, but it was discovered
that having self-synchronized collection classes rarely is enough to provide
thread safety, rather synchronization usually has to be handled in a larger
scope than the individual method call. Therefore, if you think Vector makes
your code thread-safe, you need to think again. Even if works it usually a
very expensive way of accomplishing the goal.

-- robin

--
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc


pgsql-jdbc by date:

Previous
From: Robin Rosenberg
Date:
Subject: Re: Cleanup patch: Change from Vector/Hashtable to ArrayList/HashMap
Next
From: mephysto
Date:
Subject: Postgres JDBC, WS and commit