Skip to main content

APIzation: Replication Package

[Q#38876379][A#38876591] how to add value into List<Map<String, Object>>?

https://stackoverflow.com/q/38876379

I'm trying to add a value to an ArrayList. Here is the code I wrote: But, this does not work. Anybody have an idea?

Answer

https://stackoverflow.com/a/38876591

APIzation

List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("foo", "bar");
list.add(map);
package com.stackoverflow.api;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Human38876591 {

  public static void addValueToListMap(
    List<Map<String, Object>> list,
    String key,
    Object value
  ) {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put(key, value);
    list.add(map);
  }
}

package com.stackoverflow.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * how to add value into List<Map<String, Object>>?
 *
 * @author APIzator
 * @see <a href="https://stackoverflow.com/a/38876591">https://stackoverflow.com/a/38876591</a>
 */
public class APIzator38876591 {

  public static void addValue(Map<String, Object> map) throws Exception {
    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    list.add(map);
  }
}