0% found this document useful (0 votes)
82 views1 page

Java - Using Vectors and Hashtables: Vector Example Hashtable Example

The document discusses using Vectors and Hashtables in Java. Vectors are ideal for storing lists where searching is not needed, while Hashtables allow very fast access to key-value pairs. The document provides examples of storing Strings in a Vector and key-value pairs in a Hashtable. It also shows an example of a Hashtable where the values are Vectors, allowing storage of multiple values per key.

Uploaded by

nagvarahala
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views1 page

Java - Using Vectors and Hashtables: Vector Example Hashtable Example

The document discusses using Vectors and Hashtables in Java. Vectors are ideal for storing lists where searching is not needed, while Hashtables allow very fast access to key-value pairs. The document provides examples of storing Strings in a Vector and key-value pairs in a Hashtable. It also shows an example of a Hashtable where the values are Vectors, allowing storage of multiple values per key.

Uploaded by

nagvarahala
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Java Using Vectors and Hashtables

Vector Example
import java.util.Enumeration; import java.util.Vector;

Hashtable Example
import java.util.Enumeration; import java.util.Hashtable;

// Store Strings in a vector Vector<String> v = new Vector<String>(); v.add("Football"); v.add("Basketball"); v.add("Tennis");

// Store Strings for the key & Strings for the values Hashtable<String,String> ht = new Hashtable<String,String>(); ht.put("123", "Bob"); ht.put("456", "Jane"); ht.put("789", "Lulu");

// Get first and second elements in the vector String firstSport = v.firstElement(); String secondSport = v.get(1);

// Print all contents of the vector Enumeration<String> sports = v.elements(); while (sports.hasMoreElements()) { String sport = sports.nextElement(); System.out.println(sport); }

// Look up a key in the hashtable String key = "123"; String name = ht.get(key); if (name == null) System.out.println(key + " not found."); else System.out.println(name);

// Remove Tennis v.remove(2);

// Print all contents of the hashtable Enumeration<String> keys = ht.keys(); while (keys.hasMoreElements()) { key = keys.nextElement(); name = ht.get(key); System.out.println(key + " : " + name); }

// Remove all v.removeAllElements(); // Remove Jane ht.remove("456"); // Remove everyone ht.clear();

Vectors are ideal for storing lists of items where you typically do not need to search through the list for a specific item. Hashtables are ideal for storing key-value pairs. Access to any key-value is very fast when searching by key unlike Vectors which require a search through the entire Vector.

Hashtable of Vectors Example


// Store Strings for the key and Vectors of // Strings for the values Hashtable<String,Vector<String>> ht = new Hashtable<String,Vector<String>>(); Vector<String> v = new Vector<String>(); v.add("Denver"); v.add("Colorado Springs"); v.add("Fort Collins"); ht.put("CO", v); v = new Vector<String>(); v.add("Little Rock"); v.add("Searcy"); v.add("Hot Springs"); ht.put("AR", v); v = new Vector<String>(); v.add("Richmond"); v.add("Norfolk"); v.add("Virginia Beach"); ht.put("VA", v);

// Look up a key in the hashtable String key = "CO"; Vector<String> cities = ht.get(key); if (cities == null) System.out.println(key + " not found."); else System.out.println(cities);

// Print all contents of the hashtable Enumeration<String> keys = ht.keys(); while (keys.hasMoreElements()) { key = keys.nextElement(); cities = ht.get(key); System.out.println(key + " : " + cities); } // Remove AR entry ht.remove("AR"); // Remove all ht.clear();

You might also like