0

I have a HashTable of Stack objects:

Hashtable<String, Stack<Integer>> ht;

I want the data structure to be something like this:

"foo" => [3,6,12]
"bar" => [5,8,1]

"foo" and "bar" are keys and the two [x,y,z]s are the stacks.

How do I do something like push an Integer onto the stack in the hash table with key "a"?

Many thanks.

ale
  • 11,636
  • 27
  • 92
  • 149
  • 1
    ht.get("a").push(myInteger), but it would had been better if you had tried this by yourself in your code – morgano Jul 19 '13 at 21:03
  • hmm.. maybe I should close.. this was too obvious to benefit others?! Many thanks. – ale Jul 19 '13 at 21:03

4 Answers4

2

Have you tried

yourHashTable.get("a").push(new Integer(2));
Zavior
  • 6,412
  • 2
  • 29
  • 38
2

You can try something like this :

if(ht.containsKey("a")) {
  ht.get("a").push(0); // push some Integer
}
else {
  Stack<Integer> stack = new Stack<Integer>();
  stack.push(0); // push some integer
  ht.put("a",stack);
}

You need to use push() of Stack.

P.S: Move to HashMap instead of HashTable if you can.

Read When should I use a Hashtable versus a HashMap?

Community
  • 1
  • 1
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
2

In order to modify a given stack, retrieve a reference to it and and add the new integer.

myHashTable.get("a").push(new Integer(7));
Shamim Hafiz - MSFT
  • 21,454
  • 43
  • 116
  • 176
0
Stack<Integer> stack = hashtable.get(key);
stack.push(myint);
hashtable.put(key, stack);

Something like this, maybe :)?

Fly
  • 810
  • 2
  • 9
  • 28