0

how do i write this as a list structure in java

In this case i want the structure to be like this, Where options is also a key in another

hashmap called styles

options[{"value":"0","label":"zero"},{"value":"1","label":"one"},
   {"value":"2","label":"two"}]

Here if i want to access options[1].value should give me 1 and options[2].label should give me two.

How can i achieve this with

LIst<Map<string><string[]>>?

Also Can i pass "options" array as one of the keys in my hash map

     protected Map<String, String[]> getValueProperties(int view, Field field) {
 Map<String, String> properties = new   HashMap<String,String[]>();             
        properties.put("options", []);
        return properties
    }

I am new to handling data in this format, any pointers will be good

0

2 Answers 2

1

I think this can do:

    List<Map<String,String>> options = new ArrayList<Map<String,String>>();

and populate as :

    Map<String, String> option1 = new HashMap<String, String>();
    option1.put("value", "0");
    option1.put("level", "zero");
    options.add(option1);
    Map<String, String> option2 = new HashMap<String, String>();
    option2.put("value", "1");
    option2.put("level", "one");
    options.add(option2);
    Map<String, String> option3 = new HashMap<String, String>();
    option3.put("value", "2");
    option3.put("level", "two");
    options.add(option3);

EDIT: You can populate the list in a loop as below:

   List<Map<String,String>> options = new ArrayList<Map<String,String>>();

   String[] levels = {"zero", "one", "two"};
   for(int indx = 0; indx <levels.length; indx++){
      Map<String, String> option = new HashMap<String, String>();
      option.put("value", String.valueOf(indx));
      option.put("level", levels[indx]);
      options.add(option);
   }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Yogendra, how would I do this if it was for loop where the map option1,, option2 etc is created dynamically based on the number of entries
@user1433211: Added a sample code to dynamically populate the List with Map Elements. Please check and let me know, if that helps or you need some different behavior.
1

Use this data structure:

List< Map<String, String> >

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.