1

I have a class called Varfoo that stores variables. I have another class called Replacement that uses a hashmap to replace the x into 2. With the forget method, it's meant to forget replacing x with 2.

    Varfoo x = new VarFoo("x");
    Replacement s = new Replacement();
    s.put(new VarFoo("x"), new IntFoo(2));
    x.applyReplacement(s);
    s.forget(x);

Here's the forget method:

public boolean forget(VarFoo var) {
    if (var == null) {
        throw new NullPointerException();
    } else {
        if (replacementMap.containsKey(var)) {
            replacementMap.remove(var);
            return true;
        } else {
            return false;
        }
    }
}

It will result to null because I've removed the key itself, not what I intended. How do I revert it back to how it was?

Equals and hashcodes of Varfoo:

@Override
public boolean equals(Object o) {
    if (o == null) return false;
    if (!(o instanceof VarFoo))
        return false;
    if (o == this)
        return true;
    return name.equals(((VarFoo) o).name);
}

@Override
public int hashCode() {
    int prime = 31;
    int result = 1;
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    return result;
}
4
  • Could you explain more, that's unclear ? Did you implement hashcode and equals in your Varfoo class ?
    – azro
    Commented Jan 14, 2020 at 21:39
  • 5
    You'll need to keep a history of previous values. Commented Jan 14, 2020 at 21:39
  • Possibly a duplicate of this?
    – Jeff Holt
    Commented Jan 14, 2020 at 21:42
  • @azro Added them.
    – reiko1879
    Commented Jan 14, 2020 at 21:46

1 Answer 1

2

You can make a new implementation of Map, that contains two (e.g.) HashMaps. In normal mode it forwards every operation to map1. This is the original map. map2 is null.

When you make a savepoint, you assign an empty map to map2. get operations now go first to map2 and then, if not found, to map1. put operations go only to map2. When you call forget, you assign again null to map2.

Of course, you must implement all the other methods of the Map interface. But this should be a simple task. Take care of removes, if needed (maybe you will need a Set of removed keys.

Hint: You can use java.util.AbstractMap as a base for your implementation.

4
  • This answer can be extended to use a stack of maps, allowing arbitrarily many "save points" to be set and later reverted to. The time complexity of operations like get and containsKey operations will depend on the number of "save points" currently active.
    – kaya3
    Commented Jan 14, 2020 at 22:12
  • Is there any written example of this? Sounds like a good idea.
    – reiko1879
    Commented Jan 14, 2020 at 22:15
  • @reiko1879: Sorry, I do not know an example. I did not search for a generally available implementation of my idea.
    – Donat
    Commented Jan 14, 2020 at 22:59
  • It is pretty straightforward. You create a class, implement Map and all its methods by just forwarding the call to the current actual internal map. Those internal maps you can maintain as field, for example stored in a List<Map<...>>. And then you add those extra methods to manage them.
    – Zabuzard
    Commented Jan 14, 2020 at 22:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.