0
HashMap<String, int[]> H = new HashMap<String, int[]>(); 
H.put("drdetroit", new int[]{1,2});
H.put("drdetroit", new int[]{1,3}); 
System.out.println(H.get("drdetroit").toString());

It prints out

[I@c3c749

I assume this is the hashed value(is it?). How can I make it print my actual values?

  • If you want to print the values of an array using `toString()` on that array, you will get something completely different from the values, it does not matter if it is in a HashMap or not. Use `Arrays.toString(array)` instead – andreih Dec 05 '12 at 10:06
  • Also your hashmap will only print the first value so don't get confused. :) – Henrik Andersson Dec 05 '12 at 10:08
  • @limelights: How can I make it print both of them= – Wuschelbeutel Kartoffelhuhn Dec 05 '12 at 10:09
  • You have to change the second key to something unique since HashMaps stores only unique keys. So `H.put('drdetroit', value);` and `H.put('drchicago', value);` will work for you – Henrik Andersson Dec 05 '12 at 10:10
  • @limelights: Is there an object that I can use that allows multiple values per key? – Wuschelbeutel Kartoffelhuhn Dec 05 '12 at 10:11
  • 1
    Nah, but you can do a map with a list. So `HashMap> H = new HashMap>();` and then add the values to the list. I'll post an answer for you to see. – Henrik Andersson Dec 05 '12 at 10:12
  • @limelights If he stores an array of `int`, wouldn't he need a `List` instead of a `List` of arrays of `int` ? – andreih Dec 05 '12 at 10:16
  • I'm guessing he needs the arrays for something so the List can store the int array just fine. There are perhaps better ways of doing it but I'm working off his example now. :) – Henrik Andersson Dec 05 '12 at 10:18
  • Possible duplicate of [How do I print my Java object without getting "SomeType@2f92e0f4"?](http://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4) – Kevin Panko Dec 16 '15 at 22:06

5 Answers5

2

You use Arrays.toString(H.get("drdetroit"));

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
Shark
  • 6,513
  • 3
  • 28
  • 50
2

This is what you're after I believe. Since a HashMap can only store unique keys you're forced to go with a list if you want multiple values for one (1) key.

There are perhaps better ways of achieving this but it's working and is pretty expressive in itself and pretty clear what it does and how.

The HashMap now accepts the List interface and can thusly accept any list-type that implements it. Which is pretty neat! :)

ArrayList<int[]> values = new ArrayList<int[]>();

values.add(new int[]{1,2});
values.add(new int[]{1,3});

HashMap<String, List<int[]>> H = new HashMap<String, List<int[]>>(); 

H.put("drdetroit", values);

for(String key : H.keySet()) {
    for(int[] array : H.get(key)){
        System.out.println(Arrays.toString(array));
    }
}
Henrik Andersson
  • 45,354
  • 16
  • 98
  • 92
1
System.out.println(Arrays.toString(H.get("drdetroit")));

I - stands for int array and c3c749 is hashcode

Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
0

Because you try to make a String out of an array!

You could do for example:

String myArray = H.get("drdetroit");
System.out.println(myArray[0]);
cruxi
  • 819
  • 12
  • 28
0

Try this to print array values -

System.out.println(Arrays.toString(H.get("drdetroit")));

H.get("drdetroit").toString() returns array class name and @ and hashcode in hexString.

public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103