Core JAVA

Write Once, Run Anywhere

  • Java Basic
Java - Overview
Java - Environmental Setup
First Step towards Java Programming
Importing Classes
Java - Basic Datatypes
Java - Variable Types
Java - Modifier types
Java - Basic Operators
Java - Loop Control
Java - Decision Making
Java - Numbers
Java - Characters
Java - Strings
Java - Arrays
Java - Date & Time
Java - Regular Expressions
Java - Methods
Java - Files and I/O
Java - Exceptions
  • Java Object Oriented
Java - Inheritance
Java - Overriding
Java - Polymorphism
Java - Abstraction
Java - Encapsulation
Java - Interfaces
Java - Packages
  • Java Advanced
Java - Data Structures
Java - Collections
Java - Serialization
Java - Networking
Java - Multithreading
Java - Applet Basics


      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 we will discussed later:

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.

SNMethodsDescription
1boolean hasMoreElements( ) When implemented, it must return true while there are still more elements to extract, and false when all the elements have been enumerated.
2 Object nextElement( ) This returns the next object in the enumeration as a generic Object reference.

Example:

Following is the example showing usage of Enumeration.


Output:


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.

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:

SNMethodsDescription
1void 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.
Powered by Blog - Widget
Face Upward - Widget