[GR-Jug] need help with HashMap

Brian Curnow bcurnow at gfs.com
Tue Nov 18 15:11:25 EST 2003


There are at least these two ways of doing it. Either use the keySet or the entrySet. One returns a Set of Objects which are the keys to the Map, the other returns a Set of Map.Entry objects containing both keys and values.

        Map map = new HashMap();
        Object key = null;
        Map.Entry entry = null;
        map.put("TestKey1", "TestValue1");
        map.put("TestKey2", "TestValue2");
        map.put("TestKey3", "TestValue3");
        map.put("TestKey4", "TestValue4");
        map.put("TestKey5", "TestValue5");

        Iterator it = map.keySet().iterator();

        while (it.hasNext()) {
            key = it.next();

            System.out.println("key=" + key + " value=" + map.get(key));
        }

        it = map.entrySet().iterator();

        while (it.hasNext()) {
            entry = (Map.Entry) it.next();

            System.out.println("key=" + entry.getKey() + " value=" + entry.getValue());
        }



Brian Curnow
Gordon Food Service
IS - Integration Services
616-717-7786

The computer is always right.
The programmers are occasionally right.

>>> Ken Radlick <radlickk at gr-jug.org> 11/18/03 02:52PM >>>
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