How to show HashMap value?

1

I'm having a hard time showing (this may be using for ) the value of this HashMap. Does anyone know how? The console only shows this value:

  

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

I can not show the inner vector, 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'
    
asked by anonymous 04.10.2015 / 19:17

1 answer

2

It was not clear, but in its example there seems to always be a pattern , a 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 so, 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 this map , you can use the 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 :

  

Element name: input1
  Pattern: [+] \ d {2} [(] \ d {2} [)] \ d {4} [-] \ d {4}
  Type: tel
  Value: +91 98111222333

     

Element name: button1
  Pattern:
  Type: button
  Value: validate


You can use the toString method 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

    
04.10.2015 / 19:27