java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4093
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to create a Hash Table E-mail
User Rating: / 155
PoorBest 

This Java tip illustrates a method of creating a Hash Table. Hash table is generally used in searching applications. Hash table increases the search efficiency by creating a key corresponding to the values in the table.

    // Create a hash table
    Map map = new HashMap();    
    map = new TreeMap();        // sorted map
    
    // Add key/value pairs to the map
    map.put("x"new Integer(1));
    map.put("y"new Integer(2));
    map.put("z"new Integer(3));
    
    // Get number of entries in map
    int size = map.size();        // 2
    
    // Adding an entry whose key exists in the map causes
    // the new value to replace the old value
    Object oldValue = map.put("x"new Integer(9));  // 1
    
    // Remove an entry from the map and 
    // return the value of the removed entry
    oldValue = map.remove("c");  // 3
    
    // Iterate over all keys in the table 
    Iterator it = map.keySet().iterator();
    while (it.hasNext()) {
        // Retrieve key
        Object key = it.next();
    }
    
    // Iterate over all values in table 
    it = map.values().iterator();
    while (it.hasNext()) {
        // Get value
        Object value = it.next();
    }


 Related Tips

 
< Prev   Next >
 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.