Re: How do I set up CollapsableHashtable to take parameters?
phillip.s.powell@gmail.com wrote:
On Mar 20, 5:13 pm, Joshua Cranmer <Pidgeo...@epenguin.zzn.com> wrote:
phillip.s.pow...@gmail.com wrote:
On Mar 20, 12:18 pm, Tom Hawtin <use...@tackline.plus.com> wrote:
The main point of still using Hashtable instead of HashMap is that
Hashtable methods are generally synchronized. So, it would make sense
for this method to be synchronized to.
Lost me completely.
Synchronization incurs extra overhead in order to ensure that multiple
threads cannot access the data structure at the same time. If your
application is not multithreaded, then synchronization is just useless
overhead.
That entire response completely lost me, sorry.
Then look up "synchronized" and read the API docs for Hashtable vs. HashMap.
Hashtable is a legacy class, likewise Enumeration (and Vector). They were
supplanted by collections with J 1.2. Every method of Hashtable is
synchronized. Not so with HashMap. Synchronization adds overhead, necessary in
multithreaded apps but not otherwise. Collections now include synchronized
collections, so Hashtable is more obsolescent than ever.
Where did you learn about Hashtable and Enumeration anyway? They were replaced
in 2000.
Anyway, if Joshua's explanations lost you, you should read the Sun tutorial on
threads and the API docs for the classes in question. GIYF.
The easy part to take away is to use HashMap instead of Hashtable, and
Iterator instead of Enumeration.
Enumeration keys = this.keys();
Enumeration<K> would be better.
I actually implemented that after posting this, so I got one of them
right!
while (keys.hasMoreElements()) {
remove(keys.nextElement());
}
Mixing operations on a Enumeration/Iterator and the original object is
asking for trouble. Fortunately Iterator has a remove method.
I don't understand, sorry.
What he is trying to say is that mixing the methods of an Enumeration
with methods on the Hashtable it references opens up BIG problems.
I don't understand why considering I was under the impression they are
all collections.
Just because they are all collections doesn't mean they are all identical.
Read the API docs.
The API docs will help.
There is more detail on these issues in the API docs. Among other things, the
API docs warn against modifying a collection while iterating through it,
unless you use the iterator itself to do the modification.
It's all in the API docs.
Among other things, the API docs will help to clarify that Enumeration is not
a collections framework element, but Iterator is. Mixing non-collections
actions with collections actions is dangerous. The fact that Hashtable
implements Map does not prevent it from having additional, incompatible
features. There is more about each of these classes, interfaces and methods in
the API docs.
-- Lew