Results 1 to 2 of 2
  1. #1
    toby is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Help with HashMap in java

    I have a hashmap setup in the following way:
    Java 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

  2. #2
    Seemster is offline Member
    Join Date
    Jul 2007
    Posts
    55
    Rep Power
    0

    Default

    // 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

  1. HashTable vs HashMap
    By bugger in forum New To Java
    Replies: 7
    Last Post: 01-06-2011, 03:15 PM
  2. Soft HashMap
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-12-2008, 08:45 PM
  3. HashMap visual example
    By jhetfield18 in forum Advanced Java
    Replies: 1
    Last Post: 12-12-2007, 07:45 PM
  4. Hashmap
    By dirtycash in forum New To Java
    Replies: 5
    Last Post: 12-03-2007, 02:58 AM
  5. what is hashmap
    By gabriel in forum New To Java
    Replies: 5
    Last Post: 08-03-2007, 01:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •