Create a key value list in Java

1

How to create a list in Java Android of the type dictionary that is to say that content can be stored clave:valor , the key and its value are String .

Update 1

I have done the following:

Map<String, String> listParams = new HashMap<String, String>();

To add parameters:

listParams.put("offset","3");
listParams.put("items","50");
listParams.put("sort","title,asc");

To scroll through all the elements

for(Map.Entry<String, String> entry : listParams) {
    String.format("llave: %s, valor: %s", entry.getKey(), entry.getValue());
}

I get the following error

  

Test.java:37: error: for-each not applicable to expression type   for (Map.Entry entry: listParams) {                                          ^ required: array or java.lang.Iterable found: Map 1 error

    
asked by Webserveis 25.04.2016 в 19:21
source

1 answer

4

Use the Pair class instead:

List<Pair<String, String>> listParams = new ArrayList<Pair<String, String>>();

Or failing and more practical, use a Map directly, since it is already a collection of pairs of values:

Map<String, String> map = new HashMap<String, String>();

If you use Map , you can browse all the values there using the following code:

for (Map.Entry<String, String> entry : map.entrySet()) {
    System.out.println(
        String.format("llave: %s, valor: %s", entry.getKey(), entry.getValue())
    );
}
    
answered by 25.04.2016 / 19:24
source