PDA

View Full Version : [java] extract the value from a HashMap



mickey
15th August 2009, 19:24
Hello my situation is that:


Vector<Word> _words = new Vector<Word>();
//fill the vector
Map<String, Integer> tham = new HashMap();
//fill the map
..................................
for ( String w : _words) {
Collection c = tham.values();
Iterator it= c.iterator();
int value = 0;
for ( String s : tham.keySet() ) {
System.out.println(" -- string -- " + s);
if ( s.equals( w )) {
System.out.println(" -- string -- " + s);
value = (Integer) it.next();
}
else it.next();
}

}

This should work but it very slow. Is there a way to retrieve the Integer value of the Map with iterate on 'tham' all the times?

thanks,

mickey
16th August 2009, 12:04
sorry, the Vector is made by String:
Vector<String> .....

caduel
16th August 2009, 18:16
what exactly are you trying to do? didn't understand the question, sorry.

use containsKey to check if the hashmap contains an entry, or just call get - it returns null if the key was not in the hashmap.
Do not iterate over a map when you dont have to.