So I'm trying to figure out why my get method doesn't pass its test.

This is my attempt at the get method:

public Value get(Object key) { //Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
        for (Pair<Key,Value> item : list) {
            if (item.key.equals(key)) {
                return item.value; 
            }
        }

        return null;
    }

 public Value put(Key key, Value value) { //Associates the specified value with the specified key in the map.

        for (Pair<Key,Value> item : list) {
            if (containsKey(key)==true) { //A map m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true.
                return item.value; 
            }
        }
      size++;
       return null;
    }

My put method:

    public Value put(Key key, Value value) { //Associates the specified value with the specified key in the map.

            for (Pair<Key,Value> item : list) {
                if (containsKey(key)==true) { //A map m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true.
                    return item.value; 
                }
            }
          size++;
           return null;
        }

My clear method:

public void clear() {
        list.clear();
    }

The get test method:

public void testGet() {
        map.clear();
        map.put("All These Things That I Have Done","Killers");
        map.put("Human","Killers");
        map.put("Evil","Interpol");
        assertThat(map.get("Evil"), is("Interpol"));
        assertThat(map.get("Human"), is("Killers"));
    }

The test fails. I'm not sure if the get method test fails because the put or clear methods are wrong, or if it's just the get method...

This is the error I get:
java.lang.AssertionError:
Expected is: "Interpol"
but: was null

I would really appreciate any help at all, even small suggestions. I'm pretty lost if you can't tell. :(

Recommended Answers

All 5 Replies

It's your put method.
You check to see if that key is already there, and return true, but if it's not there you increment the size but you forgot to add the new key/value Pair to the map.

Okay, thanks...
Well now I have this:

    public Value put(Key key, Value value) { //Associates the specified value with the specified key in the map.

            for (Pair<Key,Value> item : list) {
                if (containsKey(key)==true) { //A map m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true.
                    item.value = value; //I'm not sure if I need this? (Even without it I still get an error)
                    return item.value; 
                }
            }
          Pair<Key, Value> newItem = new Pair<Key,Value>(key, value);
          newItem.value = value; //Do I need this? (Even without it I still get an error)
          size++;
           return newItem.value;
        }

The test still has the same error:
java.lang.AssertionError:
Expected is: "Interpol"
but: was null

You still are not putting the new key/value pair into your map. You create the Pair, but never add that to the map.

You don't say how the map is implemented, so I can't say what command(s) you would need to do that, but the size++ (line 11) tends to suggest that there may be some kind of array of Pairs somewhere that you need to add your new Pair to????

It's a list of pairs

So I tried doing

    Pair<Key, Value> newItem = new Pair<Key,Value>(key, value);
        list.add(newItem);
 size++;
           return newItem.value;

and that didn't work either. Same error.

How are the classes Key and Value defined? In your method calls you are passing Strings for both those.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.