Tuesday, March 19, 2013

Java - The Data Structures


The data structures provided by the Java utility package are very powerful and perform a wide range of functions. These data structures consist of the following interface and classes:
·         Enumeration
·         BitSet
·         Vector
·         Stack
·         Dictionary
·         Hashtable
·         Properties
All these classes are now legacy and Java-2 has introduced a new framework called Collections Framework which is discussed in next tutorial:

The Enumeration:

The Enumeration interface isn't itself a data structure, but it is very important within the context of other data structures. The Enumeration interface defines a means to retrieve successive elements from a data structure.
For example, Enumeration defines a method called nextElement that is used to get the next element in a data structure that contains multiple elements.
To have more detail about this interface, check TheEnumeration.

The BitSet

The BitSet class implements a group of bits, or flags, that can be set and cleared individually.
This class is very useful in cases where you need to keep up with a set of boolean values; you just assign a bit to each value and set or clear it as appropriate.
To have more detail about this class, check TheBitSet.

The Vector

The Vector class is similar to a traditional Java array, except that it can grow as necessary to accommodate new elements.
Like an array, elements of a Vector object can be accessed via an index into the vector.
The nice thing about using the Vector class is that you don't have to worry about setting it to a specific size upon creation; it shrinks and grows automatically when necessary.
To have more detail about this class, check TheVector.

The Stack

The Stack class implements a last-in-first-out (LIFO) stack of elements.
You can think of a stack literally as a vertical stack of objects; when you add a new element, it gets stacked on top of the others.
When you pull an element off the stack, it comes off the top. In other words, the last element you added to the stack is the first one to come back off.
To have more detail about this class, check TheStack.

The Dictionary

The Dictionary class is an abstract class that defines a data structure for mapping keys to values.
This is useful in cases where you want to be able to access data via a particular key rather than an integer index.
Since the Dictionary class is abstract, it provides only the framework for a key-mapped data structure rather than a specific implementation.
To have more detail about this class, check TheDictionary.

The Hashtable

The Hashtable class provides a means of organizing data based on some user-defined key structure.
For example, in an address list hash table you could store and sort data based on a key such as ZIP code rather than on a person's name.
The specific meaning of keys in regard to hash tables is totally dependent on the usage of the hash table and the data it contains.
To have more detail about this class, check TheHashtable.

The Properties

Properties is a subclass of Hashtable. It is used to maintain lists of values in which the key is a String and the value is also a String.
The Properties class is used by many other Java classes. For example, it is the type of object returned by System.getProperties( ) when obtaining environmental values.
To have more detail about this class, check TheProperties.

The Properties Class


Properties is a subclass of Hashtable. It is used to maintain lists of values in which the key is a String and the value is also a String.
The Properties class is used by many other Java classes. For example, it is the type of object returned by System.getProperties( ) when obtaining environmental values.
Properties defines the following instance variable. This variable holds a default property list associated with a Properties object.
Properties defaults;
The Properties defines two constructors. The first version creates a Properties object that has no default values:
Properties( )
The second creates an object that uses propDefault for its default values. In both cases, the property list is empty:
Properties(Properties propDefault)
Apart from the methods defined by Hashtable, Properties defines following methods:
SN
Methods with Description
1
String getProperty(String key)
Returns the value associated with key. A null object is returned if key is neither in the list nor in the default property list.
2
String getProperty(String key, String defaultProperty)
Returns the value associated with key. defaultProperty is returned if key is neither in the list nor in the default property list.
3
void list(PrintStream streamOut)
Sends the property list to the output stream linked to streamOut.
4
void list(PrintWriter streamOut)
Sends the property list to the output stream linked to streamOut.
5
void load(InputStream streamIn) throws IOException
Inputs a property list from the input stream linked to streamIn.
6
Enumeration propertyNames( )
Returns an enumeration of the keys. This includes those keys found in the default property list, too.
7
Object setProperty(String key, String value)
Associates value with key. Returns the previous value associated with key, or returns null if no such association exists.
8
void store(OutputStream streamOut, String description)
After writing the string specified by description, the property list is written to the output stream linked to streamOut.

The Hashtable Class


Hashtable was part of the original java.util and is a concrete implementation of a Dictionary.
However, Java 2 reengineered Hashtable so that it also implements the Map interface. Thus, Hashtable is now integrated into the collections framework. It is similar to HashMap, but is synchronized.
Like HashMap, Hashtable stores key/value pairs in a hash table. When using a Hashtable, you specify an object that is used as a key, and the value that you want linked to that key. The key is then hashed, and the resulting hash code is used as the index at which the value is stored within the table.
The Hashtable defines four constructors. The first version is the default constructor:
Hashtable( )
The second version creates a hash table that has an initial size specified by size:
Hashtable(int size)
The third version creates a hash table that has an initial size specified by size and a fill ratio specified by fillRatio.
This ratio must be between 0.0 and 1.0, and it determines how full the hash table can be before it is resized upward.
Hashtable(int size, float fillRatio)
The fourth version creates a hash table that is initialized with the elements in m.
The capacity of the hash table is set to twice the number of elements in m. The default load factor of 0.75 is used.
Hashtable(Map m)
Apart from the methods defined by Map interface, Hashtable defines following methods:
SN
Methods with Description
1
void clear( )
Resets and empties the hash table.
2
Object clone( )
Returns a duplicate of the invoking object.
3
boolean contains(Object value)
Returns true if some value equal to value exists within the hash table. Returns false if the value isn't found.
4
boolean containsKey(Object key)
Returns true if some key equal to key exists within the hash table. Returns false if the key isn't found.
5
boolean containsValue(Object value)
Returns true if some value equal to value exists within the hash table. Returns false if the value isn't found.
6
Enumeration elements( )
Returns an enumeration of the values contained in the hash table.
7
Object get(Object key)
Returns the object that contains the value associated with key. If key is not in the hash table, a null object is returned.
8
boolean isEmpty( )
Returns true if the hash table is empty; returns false if it contains at least one key.
9
Enumeration keys( )
Returns an enumeration of the keys contained in the hash table.
10
Object put(Object key, Object value)
Inserts a key and a value into the hash table. Returns null if key isn't already in the hash table; returns the previous value associated with key if key is already in the hash table.
11
void rehash( )
Increases the size of the hash table and rehashes all of its keys.
12
Object remove(Object key)
Removes key and its value. Returns the value associated with key. If key is not in the hash table, a null object is returned.
13
int size( )
Returns the number of entries in the hash table.
14
String toString( )
Returns the string equivalent of a hash table.
Example:
The following program illustrates several of the methods supported by this data structure:
import java.util.*;

public class HashTableDemo {

   public static void main(String args[]) {
      // Create a hash map
      Hashtable balance = new Hashtable();
      Enumeration names;
      String str;
      double bal;

      balance.put("Zara", new Double(3434.34));
      balance.put("Mahnaz", new Double(123.22));
      balance.put("Ayan", new Double(1378.00));
      balance.put("Daisy", new Double(99.22));
      balance.put("Qadir", new Double(-19.08));

      // Show all balances in hash table.
      names = balance.keys();
      while(names.hasMoreElements()) {
         str = (String) names.nextElement();
         System.out.println(str + ": " +
         balance.get(str));
      }
      System.out.println();
      // Deposit 1,000 into Zara's account
      bal = ((Double)balance.get("Zara")).doubleValue();
      balance.put("Zara", new Double(bal+1000));
      System.out.println("Zara's new balance: " +
      balance.get("Zara"));
   }
}
This would produce following result:
Qadir: -19.08
Zara: 3434.34
Mahnaz: 123.22
Daisy: 99.22
Ayan: 1378.0

Zara's new balance: 4434.34