Results 1 to 2 of 2
Thread: Help with HashMap in java
- 07-24-2007, 05:09 AM #1
Member
- Join Date
- Jul 2007
- Posts
- 40
- Rep Power
- 0
Help with HashMap in java
I have a hashmap setup in the following way:
I currently can check if a given value is in the map no problem. But once I determine that a value is in the map, how do I return the key or keys at which a given value was found? This is crucial for me to be able to do. I know values are not unique but there has to be a way to return which key or keys have that value.Java Code:Key Value 0 XXX 1 XYX 2 XYY 3 XXX 4 YXX 5 YXX
Thanks
- 07-25-2007, 08:04 PM #2
Member
- Join Date
- Jul 2007
- Posts
- 55
- Rep Power
- 0
// Create a hash table
Map map = new HashMap(); // hash table
map = new TreeMap(); // sorted map
// Add key/value pairs to the map
map.put("a", new Integer(1));
map.put("b", new Integer(2));
map.put("c", 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("a", 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 the keys in the map
Iterator it = map.keySet().iterator();
while (it.hasNext()) {
// Get key
Object key = it.next();
}
// Iterate over the values in the map
it = map.values().iterator();
while (it.hasNext()) {
// Get value
Object value = it.next();
}
Similar Threads
-
HashTable vs HashMap
By bugger in forum New To JavaReplies: 7Last Post: 01-06-2011, 03:15 PM -
Soft HashMap
By Java Tip in forum java.langReplies: 0Last Post: 04-12-2008, 08:45 PM -
HashMap visual example
By jhetfield18 in forum Advanced JavaReplies: 1Last Post: 12-12-2007, 07:45 PM -
Hashmap
By dirtycash in forum New To JavaReplies: 5Last Post: 12-03-2007, 02:58 AM -
what is hashmap
By gabriel in forum New To JavaReplies: 5Last Post: 08-03-2007, 01:23 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks