0
final HashMap<Integer, HashMap<Integer, Integer[]>> teams;
   teams= new HashMap<Integer, HashMap<Integer, Integer[]>>();


    teams.put(1,new HashMap<Integer, Integer[]>(){{
        put(2,new Integer[] { 1,0});}}); 
        System.out.println(teams.get(1).get(2));

I am trying to implement hash of hashes in java. I need to print integer array being stored in hash of a hash. Any help in this regard would be appreciated.

  • 1
    How would you print an `Integer[]` if it were not stored in a hash of hashes? Do the same thing at the appropriate place in the code. – Jim Garrison Nov 18 '13 at 08:26

2 Answers2

1

Your code is correct (I only re-indented here), and I added the call to the Arrays.toString method in your println call.

final HashMap<Integer, HashMap<Integer, Integer[]>> teams;
teams = new HashMap<Integer, HashMap<Integer, Integer[]>>();

teams.put(1, new HashMap<Integer, Integer[]>() {
  {
    put(2, new Integer[] { 1, 0 });
  }
});
System.out.println(Arrays.toString(teams.get(1).get(2)));
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

Note: code teams.get(1).get(2) returns type of Integer[].

teams.get(1) will get a HashMap, in the example, its key is 2 and its value is an Integer Array (Integer[]) with elements [1,0]

teams.get(1).get(2) returns values, whose type is Integer[].

You CAN NOT print its value via System.out.println(teams.get(1).get(2)) directly because toString is not implemented.

There are 2 ways for you to print the element in array as follows:

1 you can print element in array using its index,

like

     System.out.println(teams.get(1).get(2)[0]);//print 1
     System.out.println(teams.get(1).get(2)[1]);//Print 0

OR

2 Use Arrays.toString menthod to print all the elements in array,

like

     System.out.println(Arrays.toString(teams.get(1).get(2)));//Print [1,0]
Mengjun
  • 3,159
  • 1
  • 15
  • 21