Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need a Map impl which would consist of stacked maps, which I could push() and pop(), and the values would be "added" or "removed" if they belong to the map being pushed/popped. And the values would be searched top/bottom (or optionally bottom/top).

Is there an existing impl in JDK or elsewhere?

Example:

  • Stack
    • map4
      • foo => aaa
      • bar => 45
    • map3
      • bar => 22
    • map2
      • foo => ccc
      • baz => uuu
    • map1

For this, get("baz") would return "uuu", get("foo") would return "aaa", size() would return 3 etc. It's something like JavaScript's prototypal inheritance.

There's one impl I'm wishing for some more sophisticated impl, which wouldn't really go through all layers every time I call any method. Read methods are going to be more often than push()/pop(), so there could be some pre-computation during that.

share|improve this question
And how would you create the "outer maps" here? – fge Jun 16 at 20:29
There is no builtin structure in the JDK like that, but it'd be pretty easy to write an implementation of yours using, say, a LinkedList<Map<K, V>> – fge Jun 16 at 20:33
You can use a LinkedBlockingDeque<Map<K, V>> if you need the stack to be thread-safe – Zim-Zam O'Pootertoot Jun 16 at 20:38
1  
Compare with Properties, which can have another Properties map as default values. – Raedwald Jun 16 at 20:56
add comment (requires an account with 50 reputation)

2 Answers

You can have Stack as a wrapper. Have a Map<'String, Map> ( String here is for the name of the map). Expose push and pop as API. Interesting part of your question is, defining push and pop? How would the signature of these method actually look like? Actually, not very clear is to what you are trying to achieve?

share|improve this answer
add comment (requires an account with 50 reputation)

So, there is no such builtin structure in the JDK, but it can be implemented using a LinkedList containing Maps.

LinkedList implements all three of List, Queue and Deque, maybe it's a little overkill, but ohwell...

A sample code would be as follows; however, the Map interface is not really obeyed (curious how you'd do .equals() and .hashCode() here? Not even talking about .clear()):

public final class StackedMap<K, V>
    implements Map<K, V>
{
    private final Map<K, V> NO_MAP = new HashMap<K, V>();
    private final LinkedList<Map<K, V>> maps = new LinkedList<>();

    private Map<K, V> currentMap = NO_MAP;

    public void push(Map<K, V> map)
    {
        maps.push(map);
        currentMap = map;
    }

    public Map<K, V> pop()
    {
        return currentMap = maps.pop();
    }

    @Override
    public V get(K key)
    {
        V ret;

        for (final Map<K, V> map: maps)
            if ((ret = map.get(key)) != null)
                break;
        return ret;
    }

    // etc
}

Untested etc.

share|improve this answer
Regarding LinkedList being overkill, Java has a Stack class, but its documentation says "A more complete and consistent set of LIFO stack operations is provided by the Deque interface and its implementations, which should be used in preference to this class" – Zim-Zam O'Pootertoot Jun 16 at 20:50
@fge how about having has-a relationship than is-a? I was thinking it would be a better thing to do considering the question – zerocool Jun 16 at 20:54
Sure I can write it myself, but I'm wishing for some more sophisticated impl, which wouldn't really go through all layers every time I call any method. Read methods are going to be more often than push()/pop(), so there could be some pre-computation durint that. – Ondra Žižka Jun 16 at 20:55
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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