user1382202
user1382202

Reputation: 31

Printing HashMap values (those values are Arrays) error

I've been trying and seeking for questions which are similar to mine for almost 2 days hope I will find out the answer, but no, I couldn't so I decided to ask you guys here.

This method is to print out all the keys and values of a HashMap<String, int[]> ratingmap. So keys are Strings and values are arrays. I've been working on that and below is my code.

public void showRatingsMap() {
    for (String customer: ratingmap.keySet()) {
        String key = customer.toString();
        int[] value = ratingmap.get(key);
        System.out.println("Customer: " + key + " - Rating: " + value);
    }
}

I'm really confused at the moment because the result which is printed out looks like this:

Customer: Douglas Anderson - Rating: [I@4f5b571e
Customer: Sidney - Rating: [I@75b49b45
Customer: Apollo - Rating: [I@243e0b62
Customer: Leslie - Rating: [I@655d6184

As I expect the rating to be an array, but it always appears as the weird combination above: [I@2b9fd66a

Could anyone please point out the mistakes that cause the problem?

Upvotes: 3

Views: 4775

Answers (4)

Paul Bellora
Paul Bellora

Reputation: 55223

Arrays don't override the default toString() method. In order to print a representation of their contents, you can use Arrays.toString(int[]).

See my answer here for more details.

Upvotes: 6

Victor Sorokin
Victor Sorokin

Reputation: 12006

What you see is the result of built-in toString on integer array types -- it prints "type of array" [I part and "reference" part, e.g., @75b49b45, which differs on per-instance basis inside the same JVM.

What you need is to manually iterate over array items and print each separately:

    int array[] = new int[] {3, 1, 4, 1};
    for (int i : array)
        System.out.print(i + " ");
    System.out.println();

Or you may convert array to List which has more friendly toString() implementation:

    int array[] = new int[] {3, 1, 4, 1};
    List<Integer> asList = new ArrayList<Integer>();

    for (int i : array)
        asList.add(i);
    System.out.println(asList);

Regarding starting point, there's explanation why you see [I -- it's because arrays have built-in toString which is inherited from toString() implementation of Object class and the latter works by printing type code of class it's called upon, then @ then internal id of instance of class. And type code for integer arrays is [I.

See full description of possible of type codes in javadoc of Class#getName() in JDK.

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533560

The simplest solution is to use HashMap<String, List<Integer>> as this will print as you might expect.

Upvotes: 1

Luciano
Luciano

Reputation: 8582

Value is "int[]", an array of ints. But when you convert that object to string, it doesn't automatically print all the elements of the array for you. It only prints the position in memory of the array, which is what you see.

You need to iterate over the elements of the array in order to print them. Like this:

for (int v : value) {
    System.out.println(v);
}

Or, as Paul said (Use a Helper class, like Arrays)

Upvotes: 2

Related Questions