0

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.

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277

2 Answers2

0

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?

ajay.patel
  • 1,957
  • 12
  • 15
0

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.

fge
  • 119,121
  • 33
  • 254
  • 329
  • 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 '13 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 – ajay.patel Jun 16 '13 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 '13 at 20:55