You are on page 1of 10

L4.

Necessary Java Programming Techniques


(Hashtable)

Java API

Hierarchy Java API


java.util Class Hashtable java.lang.Object java.util java.util.Hashtable
This class implements a hashtable, which maps keys to values. Any non-null object can be used as a key or as a value.
2

Constructor Methods
Hashtable()
Constructs a new, empty hashtable with a default initial capacity (11) and load factor, which is 0.75.

Hashtable(int initialCapacity)
Constructs a new, empty hashtable with the specified initial capacity and default load factor, which is 0.75.

Hashtable(int initialCapacity, float loadFactor)


Constructs a new, empty hashtable with the specified initial capacity and the specified load factor.

Hashtable(Map t)
Constructs a new hashtable with the same mappings as the given Map.
public interface Map An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

A simple example
This example creates a hashtable of numbers. It uses the names of the numbers as keys:
Hashtable numbers = new Hashtable(); numbers.put("one", new Integer(1)); numbers.put("two", new Integer(2)); numbers.put("three", new Integer(3));

To retrieve a number, use the following code:


Integer n = (Integer)numbers.get("two"); if (n != null) { System.out.println("two = " + n); }
4

Methods
void clear()
Clears this hashtable so that it contains no keys.

boolean contains(Object value)


Tests if some key maps into the specified value in this hashtable.

boolean isEmpty()
Tests if this hashtable maps no keys to values.

boolean containsKey(Object key)


Tests if the specified object is a key in this hashtable.

boolean containsValue(Object value)


Returns true if this Hashtable maps one or more keys to this value.

Enumeration elements()
Returns an enumeration of the values in this hashtable.

Enumeration keys()
Returns an enumeration of the keys in this hashtable.
5

Methods
Object get(Object key)
Returns the value to which the specified key is mapped in this hashtable.

Object put(Object key, Object value) Maps the specified key to the specified value in this hashtable. Objec remove(Object key)
Removes the key (and its corresponding value) from this hashtable

Set keySet()
Returns a Set view of the keys contained in this Hashtable

public interface Set extends Collection


A collection that contains no duplicate elements.

import java.util.Hashtable; import java.util.Vector; import java.util.Collections; import java.util.Enumeration;

An example: Sort Hashtable


import java.util.*;

Key ABC XYZ

Value abc xyz

public class SortHashtable { public static void main(String[] args) { // Create and populate hashtable Hashtable ht = new Hashtable(); ht.put("ABC", "abc"); ht.put("XYZ", "xyz"); ht.put("MNO", "mno"); // Sort hashtable. Vector v = new Vector(ht.keySet()); Collections.sort(v);

MNO

mno

static voidsort(List list)


Sorts the specified list into ascending order, according to the natural ordering of its elements

// Display (sorted) hashtable. for (Enumeration e = v.elements(); e.hasMoreElements();) { String key = (String)e.nextElement(); String val = (String)ht.get(key); System.out.println("Key: " + key + " Val: " + val); } } }

Output:
Key: ABC Val: abc Key: MNO Val: mno Key: XYZ Val: xyz
7

Demo

Exercise
Understand and run the java program in page7.
SortHashTable.java

Look at the following class/interface from Java documentation site.


Hashtable Set Collections

Exercise (for next week)


Make a java program that can store employees data (name and id) to a hash table and do the operations shown in the display below.

HashTableApplet.java

10

You might also like