1 / 10

L4. Necessary Java Programming Techniques

L4. Necessary Java Programming Techniques. (Hashtable). Java API. Hierarchy Java API. java.util Class Hashtable java.lang.Object java.util java.util.Hashtable.

summersc
Download Presentation

L4. Necessary Java Programming Techniques

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. L4. Necessary Java Programming Techniques (Hashtable) Java API

  2. 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.

  3. 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 interfaceMap An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

  4. 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); }

  5. Methods void clear() Clears this hashtable so that it contains no keys. boolean contains(Objectvalue)   Tests if some key maps into the specified value in this hashtable. boolean isEmpty()   Tests if this hashtable maps no keys to values. booleancontainsKey(Objectkey)   Tests if the specified object is a key in thishashtable.  booleancontainsValue(Objectvalue)   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.

  6. Methods Object get(Objectkey)   Returns the value to which the specified key is mapped in this hashtable. Object put(Objectkey, Objectvalue)    Maps the specifiedkey to the specifiedvaluein this hashtable. Objec remove(Objectkey)    Removes the key (and its corresponding value) from this hashtable SetkeySet()    Returns a Set view of the keys contained in this Hashtable public interface Set extends Collection A collection that contains no duplicate elements.

  7. An example: Sort Hashtable import java.util.Hashtable; import java.util.Vector;  import java.util.Collections;  import java.util.Enumeration;  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);           // 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);      }    }  } import java.util.*; static voidsort(List list) Sorts the specified list into ascending order, according to the natural ordering of its elements Output: Key: ABC Val: abc Key: MNO Val: mno Key: XYZ Val: xyz

  8. Demo

  9. Exercise SortHashTable.java • Understand and run the java program in page7. • Look at the following class/interface from Java documentation site. Hashtable Set Collections

  10. 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

More Related