Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-24-2007, 07:09 AM
Member
 
Join Date: Jul 2007
Posts: 40
toby is on a distinguished road
Help with HashMap in java
I have a hashmap setup in the following way:
Code:
Key Value 0 XXX 1 XYX 2 XYY 3 XXX 4 YXX 5 YXX
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.
Thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-25-2007, 10:04 PM
Member
 
Join Date: Jul 2007
Posts: 55
Seemster is on a distinguished road
// 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();
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Soft HashMap Java Tip java.lang 0 04-12-2008 10:45 PM
HashMap visual example jhetfield18 Advanced Java 1 12-12-2007 09:45 PM
Hashmap dirtycash New To Java 5 12-03-2007 04:58 AM
HashTable vs HashMap bugger New To Java 3 11-29-2007 02:28 PM
what is hashmap gabriel New To Java 5 08-03-2007 03:23 PM


All times are GMT +3. The time now is 11:33 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org