How to show Hashmap value?

Asked

Viewed 475 times

1

I’m having trouble showing (can be using a for) the value of this Hashmap. Does anyone know how? The console only shows this value:

{Joao=[Ljava.lang.String;@2b05039f}

I can’t show the internal vector, the String[]:

HashMap<String, String[][]> hm = new HashMap<String,String[][]>();          
            hm.put("joao", new String[][]{
                    {"joao", "joao"}
            } );

What I want is to represent a data structure like this:

array
  0 => 
    array
      'input1' => 
        array 
          'pattern' => string '[\+]\d{2}[\(]\d{2}[\)]\d{4}[\-]\d{4}'
          'type' => string 'tel'
          'value' => string '+91 98111222333'
  1 => 
    array
      'button1' => 
        array
          'pattern' => string '
          'type' => string 'button'
          'value' => string 'validate'

1 answer

2


It was not clear, but in his example apparently there is always a Pattern, one type and a value. So why not create an object with these attributes?

public class Element {
    private String pattern, type, value;

    public Element(String pattern, String type, String value) {
        this.pattern = pattern;
        this.type = type;
        this.value = value;
    }

    // get, set
}

And then, instead of creating a monstrous structure of HashMap<String, HashMap<String, HashMap<... you would have a simple map of Element. For example:

// pattern, type, value
Element input = new Element("[\\+]\\d{2}[\\(]\\d{2}[\\)]\\d{4}[\\-]\\d{4}", "tel", "+91 98111222333");
Element button  = new Element("", "button", "validate");

HashMap<String, Element> map = new HashMap(){{
    put("input1", input);
    put("button1", button);
}};

To go through that map, can use the interface Entry:

for(Entry<String, Element> each : map.entrySet()){

  // No caso, a 'key' é a String com o nome do elemento.
  System.out.println("Nome do elemento: " + each.getKey());

  // E o 'value' é o nosso objeto 'Element'.
  Element current = each.getValue();
  System.out.println("Pattern: " + current.getPattern());
  System.out.println("Type: " + current.getType());
  System.out.println("Value: " + current.getValue());
}

ouput:

Name of the element: input1
Pattern: [+]\d{2}[(]\d{2}[)]\d{4}[-]\d{4}
Type: tel
Value: +91 98111222333

Name of the element: button1
Pattern:
Type: button
Value: validate



Answer before the question is edited:


You can use the method toString of Arrays:

System.out.println(Arrays.toString(hm.get("joao")));

output:

[10, 2015, 10101]


Or you can go through the array normally:

for(String each : hm.get("joao"))
    System.out.println(each);

output:

10
2015
10101

  • I tested it in Eclipse and it continues with the same error :(

  • updated with current code

  • In PHP it would be simple to create that structure, now in Java I’m breaking Kuka

  • System.out.println(hm);

  • Ah, now that I see you’ve edited the question and changed the structure.

  • Yeah, it’s hard to create that simple structure :(

  • I was editing my answer but you edited it again

  • I just made it clear :)

  • I edited again.

  • I deleted the question there. Very good your answer.

  • What if my page has 100 inputs? I will need to instantiate 100x?

  • But input1 is only the key to finding the object of map, can be any name there. If your page has 100 inputs, you can instantiate a single and loop to add it 100 times in the map. Do you really need 100 elements within the collection? Wouldn’t it be more feasible to group elements with identical properties? Anyway, that’s another question.

  • True. You’re right. Thank you

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.