YFeizi
YFeizi

Reputation: 1528

Create a list of array

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 ?

Upvotes: 0

Views: 83

Answers (3)

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85809

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("--------------------------");
}

Upvotes: 4

Arthur Eirich
Arthur Eirich

Reputation: 3658

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?

Upvotes: 3

Sam Bains
Sam Bains

Reputation: 548

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)

Upvotes: 3

Related Questions