Collections

The Interfaces


Java Colleciton Interfaces

CREATED 2020-10-02 06:53:14.0

010-00-03-43

UPDATED 2020-10-02 06:53:25.0

Iterator or Enumeration...


I saw this question before... What is the difference between an Enumeration and an Iterator?

Well, lets rephrase the question and maybe it will answer itself... What is the difference between the Old Enumeration and an Iterator?

A developer of the CAFE BABE I'm not... but in my java travels I have noticed that the Old Enumeration was the original iterator back in java 1 while Iterator came out in java 2.

First: The Iterator employs a later technology called fail-fast. If an entity modifies the underlying data set or hash set while it is being traversed... it throws a
ConcurrentModificationException
...unless the iterator is the one doing the modifications. Enumeration does not do this.

Second: The methods hasMoreElements() and nextElement() of the Enumeration were shortened to hasNext() and next() in the Iterator... a sign of growing up.

Third: The Iterator provides methods for removing items from the underlying data structure while Enumeration does not.

So... in conclusion... I would say the Iterator is a modified Enumeration... or a more mature version of it... I'm just sayin...

CREATED 2020-09-28 07:23:22.0

010-00-03-3E

UPDATED 2020-10-02 07:01:02.0

HashTable


Note: This class is obsolete - this section illustrates how it and it's replacements operate.

HashTable maps Keys to Values as in Key/Value pairs. This class has been in the java paradigm since version 1 however, it was retrofitted to implement the Map interface in java 1.2 which makes it part of the Java Collection Series... how cool is that.

Hash Table

A Hash Table works with what they call buckets which are object holders or spaces for objects i.e. data. Each bucket has a Key mapped to it. The Key is how to find the data in the bucket.

The Keys must implement the hashCode and equals methods.

When you want the object or data in a particular bucket, you ask for it using the Key. Hashtable will give you the contents of the bucket that matches that key.

If you want to find an object in the table i.e. a bucket... and the table is defined using simple objects types such as String or Integer, you can use the contains(Object), containsKey(Object) or containsValue(Object) methods.

However, using containsValue(Object) on more complex data types... your mileage may vary. This is because the Hash table creates a "hash" of the object using the hashcode method. Two classes with the exact same data members are going to have different hashcodes. So you would need a reference to the original object stored... thank goodnes for the keys!

If you want to retrieve an object in a bucket... not by key... but by a data member of the object you will need to search for it i.e. you need to traverse the collection or Iterate over it. However with HashTable you can't do a sequential search through the buckets. You need to get an iterator and use that to search through the buckets.

With Hashtable you can get an Iterator from the keys() method which is a map of the keys OR an Enumeration from the elements() method which is a map of the buckets themselves.

Hashtable is thread safe because it is synchronized.

Although I have fond memories of HashTable... we have to put that in the past now and move on... because this class in now considered obsolete.

The good news is HashTable is being replaced by two classes. That's how good HashTable really is... it took two classes to replace it! HashMap and ConcurrentHashMap. For un-synchronized (non-thread safe) implementations use HashMap for synchronized implementations (thread safe) use ConcurrentHashMap.

CREATED 2020-09-28 05:46:30.0

010-00-03-3D

UPDATED 2020-10-02 06:53:26.0

HashMap


HashMap employs the Map interface and is the follow on or replacement of HashTable (for unsynchronized operations).

This class works basically the same as HashTable in that it has Keys mapped to Buckets. But it has been updated and there are some differences in the API. For example the contains(Object) method is no longer there.

HashMap is not synchronized... therefore, it is not thread safe. That means if two threads attempt to add or remove items from the underlying collection (the buckets) the future could hold unpredicatble results and that would be bad. For a theadsafe version use ConcurrentHashMap.

CREATED 2012-09-30 20:29:01.0

00-17-42

UPDATED 2020-10-02 06:53:27.0

ConcurrentHashMap


That makes ConcurrentHashMap the New and Imporved HashTable (for synchronized operations).

ConcurrentHashMap is a replacement for HashTable (for synchronized operations).

It's like this:

  • ConcurrentHashMap... a rewrite of HashTable... was developed for the java collections framework...
  • HashTable was retrofitted to implement the Map interface... (so it can be a part of the collection.

CREATED 2020-09-28 05:26:21.0

010-00-03-34

UPDATED 2020-10-02 06:53:31.0

DBID: db.wam

Page Server: Ithica

©2012 Leistware Data Systems

      Hello anonymous