如何从Java Maps(多级关联数组)获取键,值

时间:2013-12-25 09:51:05

标签: java

我有以下数组(地图):

Map<String, String[]> fields = new HashMap<String, String[]>();
fields.put( "memberDOB", new String[]{ "true", "", "", "2", "date"} );
fields.put( "memberDOJ", new String[]{ "true", "", "", "2", "date"} );
fields.put( "membershipDate", new String[]{ "true", "", "", "2", "date"} );
fields.put( "txtAccountNo", new String[]{ "true", "1", "10", "1", "int"} );
fields.put( "txtMemIntro1Address", new String[]{ "true", "5", "", "1", "string"} );

   validate validateF = new validate();
      if(validateF.validate_fields(fields)){
          //validated
      }

检索值和键:

public boolean validate_fields(Map<String, String[]> fields) {
       //How to loop through each key
        return true;
    }

3 个答案:

答案 0 :(得分:1)

您可以使用:

for (String key : fields.keySet()) {
    // key iterate over all keys in fields
}

答案 1 :(得分:1)

Iterator iterator=fields.keySet().iterator();
while(iterator.hasNext()) {
   iterator.next();
  // logic here
}

此外,您可以按照以下方式迭代值和键入

for (Map.Entry<String, String[]> entry : map.entrySet()) {
    System.out.println("Key : " + entry.getKey() + " Value : "
        + entry.getValue());
}

答案 2 :(得分:1)

Java自1.5以来每次迭代都有, 所以你可以像这样循环每个键:



    for (String key : fields.keySet()) {
        String[] value = fields.get(key);
        // process the value
    }