0

I am a beginner in Java programming. I want to create a HashMap whose values are String[]'s.

Suppose I have a JTable parametersTable with values in columns 2, 3, and 4 that I wish to fetch from each row. Each three-value set constitutes a HashMap value (String[]).

HashMap<String, String[]> map = new HashMap<String, String[]>();
String[] row = {"one", "two", "three", "four", "five", "six", "seven","eight","nine","ten","eleven"};
for (int i=0; i<row.length; i++) {
map.put(row[i], new String[] {
   (String) parametersTable.getValueAt(i, 2),
   (String) parametersTable.getValueAt(i, 3),
   (String) parametersTable.getValueAt(i, 4)});
}
System.out.println(map);

The above code outputs:

{nine=[Ljava.lang.String;@187930f1, six=[Ljava.lang.String;@2a8000ab, four=[Ljava.lang.String;@4436b0fd, one=[Ljava.lang.String;@134bc965, seven=[Ljava.lang.String;@42e49d45, eleven=[Ljava.lang.String;@68cb58ea, ten=[Ljava.lang.String;@198bbc56, two=[Ljava.lang.String;@54464ee3, three=[Ljava.lang.String;@32aeff9b, five=[Ljava.lang.String;@90ed2c, eight=[Ljava.lang.String;@443d9864}

Why is the output something cryptic and not the values like I expect?

7
  • 6
    Yes you can. What is the challenge you are facing? Commented Jul 31, 2014 at 1:31
  • You might also consider the use of a guava Multimap. docs.guava-libraries.googlecode.com/git-history/v17.0/javadoc/… Commented Jul 31, 2014 at 1:58
  • 1
    You can use any sort of object as the value for a hashmap. Commented Jul 31, 2014 at 1:58
  • 3
    I would consider using a List<String> instead, but that's just me Commented Jul 31, 2014 at 2:12
  • 2
    Your problem is, that .toString() on String[] does not print what you want. But the storing and retrieving works. Yo ucan use java.util.Arrays.toString(String[]). BTW: it is not so cryptic, it will print [ for Array, then the type (I is integer and objects print the type as Lpackage.Class;. In your case String) and then the system hashcode of the array object after the @. Commented Jul 31, 2014 at 22:55

2 Answers 2

2

Why is the output something cryptic and not the values like I expect?

[Ljava.lang.String;@ is the default representation of String arrays toString() method.

Use Arrays.toString method instead.

So instead of System.out.println(map) use this to get a meaningful representation

 for(Map.Entry<String, String[]> entry : map.entrySet()) {
       System.out.println(entry.getKey() + " -> " + Arrays.toString(entry.getValue()));
 }
Sign up to request clarification or add additional context in comments.

Comments

1

Try this method :)

     static void print(HashMap<String, String[]> map) {

        StringBuilder builder = new StringBuilder();
        for(Map.Entry<String, String[]> e : map.entrySet()) {

            builder.append(e.getKey()).append(" -> [");
            boolean tmp = false;
            for(String s : e.getValue()) {
                if(tmp) {
                    builder.append(" ");
                }
                tmp = true;
                builder.append(s);

            }
            builder.append("]\n");

        }
        System.out.println(builder.toString());

    }

You got [Ljava.lang.String;@90ed2c because default method toString() for array was invoked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.