views:

4263

answers:

4

I'm trying to create a method which iterates through a hashtable and returns the key as a string, whats the best way to go about this?

EDIT: copied from comment

Sorry if I didn't make it more clear, I'm trying to do this in Java. I've created a test class

public void runprog() {
    hashMap.put("Butter", 50);
    hashMap.put("Beans", 40);
    for (Object o: hashMap.entrySet() ) {
        Map.Entry entry = (Map.Entry) o;
        System.out.println(entry.getKey() + " " + entry.getValue());
    }
}

it outputs

Butter 50
Beans 40

I've created a method which looks for a Key and returns the value

public Object findValue(String Name){
    for (Object o: hashMap.entrySet()) {
        Map.Entry entry = (Map.Entry) o;
        return entry.getValue();
    }
    return null;
}

when I look for Butter it returns 50 when i look for Beans it returns 50

+1  A: 

Set up:

 final Object sentinal = new Object();


 Map<String, Object> map = new HashMap<String, Object>(){{
  put("key1", new Object());
  put("key2", sentinal);
 }};

Given a key, find a value (lookup):

 System.out.println(map.get("key2") == sentinal);

Given a value, find it's key (reverse lookup):

 for(Map.Entry<String, Object> entry : map.entrySet()){
  if(entry.getValue() == sentinal){
   System.out.println(entry.getKey());
  }
 }

... though, if I have to do regular reverse lookups, I generally build a reverse map:

 Map<Object, String> reverse = new HashMap<Object, String>();
 for(Map.Entry<String, Object> entry : map.entrySet()){
  reverse.put(entry.getValue(), entry.getKey());
 }
 System.out.println(reverse.get(sentinal));
Aaron Maenpaa
A: 

Sorry if I didn't make it more clear, I'm trying to do this in Java. I've created a test class

    public void runprog() {
    hashMap.put("Butter", 50);
    hashMap.put("Beans", 40);
    for (Object o: hashMap.entrySet() ) {
        Map.Entry entry = (Map.Entry) o;
        System.out.println(entry.getKey() + " " + entry.getValue());
    }
}

it outputs

Butter 50
Beans 40

I've created a method which looks for a Key and returns the value

    public Object findValue(String Name){
    for (Object o: hashMap.entrySet()) {
        Map.Entry entry = (Map.Entry) o;
        return entry.getValue();
    }
    return null;
}

when I look for Butter it returns 50 when i look for Beans it returns 50

Amorpheus
I still don't understand what you're trying to achieve...
pgras
+3  A: 

Seems like homework given the example data ("Butter", "Beans")...

In your example, findValue returns the FIRST KEY in the map/table everytime. You're not even using the key (Name) which you pased in. Here is an example that fixes your problem, thought you're using maps in all the wrong way.

a better way:

// assume Name is your key, ex. "Butter" 
// No need to iterate since maps are designed for lookup
Object value = map.get(Name);

your example, fixed:

public Object findValue(String Name){
    for (Object o: hashMap.entrySet()) {
        Map.Entry entry = (Map.Entry) o;

        // THIS IS THE IMPORTANT LINE
        if(entry.getKey().equale(Name))
        {
            return entry.getValue();
        }
    }
    return null;
}
basszero
+1  A: 

Looking at your second snippet of code, you're not actually using the Name parameter anywhere. So what's happening is that the first time around the loop, the entry's value is returned - and the first value happens to be 50.

You need to check whether the key of the entry actually equals the name you're looking for...

Zarkonnen