[GR-Jug] need help with HashMap

John Tobasco tobasco at us.ibm.com
Tue Nov 18 15:20:16 EST 2003





Ken,

Try this:

package tryit;

import java.util.*;

public class TryHashMap {
      public static void main(String[] args) {
            Map myHashMap = new HashMap();

            // TreeMap would make it sorted.  But if you don't need the
sort, HashMap gives better performance.
            //Map myHashMap = new TreeMap();

            myHashMap.put(new String("key1"), new String("value1"));
            myHashMap.put(new String("key2"), new String("value2"));
            myHashMap.put(new String("key3"), new String("value3"));
            myHashMap.put(new String("key4"), new String("value4"));
            myHashMap.put(new String("key5"), new String("value5"));

            // to get a specific value
            System.out.println("value for key3: " + myHashMap.get("key3"));

            System.out.println("iterating through the entrySet: ");
            Iterator entrySetIterator = myHashMap.entrySet().iterator();
            while (entrySetIterator.hasNext()) {
                  Map.Entry entry = (Map.Entry) entrySetIterator.next();
                  String key = (String) entry.getKey();
                  String value = (String) entry.getValue();
                  System.out.println("key=" + key + " value=" + value);
            }
      }
}

Thanks,
John Tobasco
Software Engineer - SRM Development Team
Server Resource Management
IBM Global Services, Service Delivery Center - South
877-725-7558 or T/L 364-1192
tobasco at us.ibm.com


                                                                                                                                       
                      Ken Radlick                                                                                                      
                      <radlickk at gr-jug.        To:       jug at gr-jug.org                                                                
                      org>                     cc:                                                                                     
                      Sent by:                 Subject:  [GR-Jug] need help with HashMap                                               
                      jug-bounces at gr-ju                                                                                                
                      g.org                                                                                                            
                                                                                                                                       
                                                                                                                                       
                      11/18/2003 02:52                                                                                                 
                      PM                                                                                                               
                      Please respond to                                                                                                
                      radlickk,Grand                                                                                                   
                      Rapids Java Users                                                                                                
                      Group Mailing                                                                                                    
                      List                                                                                                             
                                                                                                                                       
                                                                                                                                       




Does anyone have experience using the HashMap class?  If so, I am
looking for an example of how to traverse and print the keys and values
in the form key="key value" value="value".

Your assistance would be most appreciated.

Thanks,

Ken.

_______________________________________________
Jug mailing list
Jug at gr-jug.org
http://gr-jug.org/mailman/listinfo/jug





More information about the Jug mailing list