0

i want to create a list that each row contain an array like this :

key1 ->value1
key2 ->value2
key3 ->value3  

(key and value are string)
How can i do this ?

2
  • 4
    Looks like you want a Map<String, String> rather than an array. And you will want a List<Map<String, String>>. Commented Jan 30, 2015 at 14:33
  • 1
    Can you show me an example? pls Commented Jan 30, 2015 at 14:34

3 Answers 3

4

If your key must be a String, then you want to use a Map rather than an array. And you want/need a List<Map<String, String>>.

Here's an example:

//declaring and initializing the list of maps
//line below works for Java 7
//List<Map<String, String>> listOfMaps = new ArrayList<>();
//if you want/need to use Java 6 then use the following
List<Map<String, String>> listOfMaps = new ArrayList<Map<String, String>>();

//declaring, initializing and filling a map
//line below works for Java 7
//Map<String, String> map1 = new HashMap<>();
//if you want/need to use Java 6 then use the following
Map<String, String> map1 = new HashMap<String, String>();
map1.put("name", "Luiggi");
map1.put("lastname", "Mendoza");
map1.put("maintag", "Java");

//declaring, initializing and filling another map
//line below works for Java 7
//Map<String, String> map2 = new HashMap<>();
//if you want/need to use Java 6 then use the following
Map<String, String> map2 = new HashMap<String, String>();
map2.put("name", "Foo");
map2.put("lastname", "Bar");
map2.put("maintag", "Scala"); //I have nothing against scala

//add maps into the list
listOfMaps.add(map1);
listOfMaps.add(map2);

//Show contents of the list and map
System.out.println(listOfMaps);

//Traverse each element of the List to access key and value
int index = 0;
//it is better to use Iterator or enhanced for each than using List#get(index)
for (Map<String, String> map : listOfMaps) {
    System.out.println("Printing element " + index++);
    for(Map.Entry<String, String> entry : map.entrySet()) {
        System.out.println(entry.getKey() + ": " + entry.getValue());
    }
    System.out.println("--------------------------");
}
Sign up to request clarification or add additional context in comments.

Comments

3

You can use an

ArrayList<Map<String, String>>()

This would have the following structure

  • List index 0: Key1 -> value 1, Key2 -> value 2
  • List index 1: Key3 -> value 3

So to put a key value pair:

List<Map<String, String>> list = new ArrayList<Map<String, String>>();
Map<String, String> map = new HashMap<String, String>();
map.put(key, value);
map.put(key1, value1);
list.add(map);

So to get a value from a key in the first index of the list:

String value = list.get(0).get(key)

Comments

3
Map<String, String> map = new HashMap<String, String>();
map.put("key1", "1");
map.put("key2", "2");

Map<String, String> anotherMap = new HashMap<String, String>();
anotherMap .put("key3", "3");
anotherMap .put("key4", "4");

List<Map<String, String>> listOfMaps = new ArrayList<Map<String, String>>();
listOfMaps.add(map);
listOfMaps.add(anotherMap);

Why do you need this list? What do you need to do with it? Or is this just an exercise?

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.