转换列表<map <string,object =“”>&gt;列出<string> </string> </map <string,>

时间:2013-11-07 10:46:10

标签: java

我有一个地图列表List<Map<String, Object>>.我只需要将地图列表的值移到List<String>

有人可以让我知道如何转换吗?

7 个答案:

答案 0 :(得分:1)

试试这个

   List<Map<String, Object>> maps = new ArrayList<Map<String, Object>>();       
   List<String>  listOfValue = new ArrayList<String>();       
   for(Map map: maps){ // loop through the maps
       listOfValue.addAll(map.values()); // append the values in listOfValue
   }

答案 1 :(得分:1)

你可以这样做

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

      List<Object> list=new ArrayList<Object>();
      for(Map<String,Object> i:mapList){
          list.addAll(i.values());
      }

答案 2 :(得分:1)

我建议您使用Google提供的Guava个库并在功能上执行此操作:

import com.google.common.collect.Lists;
import com.google.common.collect.Iterables;
import com.google.common.base.Function;

// ...

final List<Map<String, Object>> list = ...;
final Function<Map<String, Object>, Collection<String>> mapToKeysFunction = new Function<Map<String, Object>, Collection<String>>() {

    @Override
    public Collection<String> apply(final Map<String, Object> map) {
        return map.keySet();
    }
};
final List<Collection<String>> listOfStringCollections = Lists.transform(list, mapToKeysFunction);
final Iterable<String> stringIterable = Iterables.concat(listOfStringCollections);
// either use string iterable here directly or convert it to List if necessary
final List<String> result = Lists.newArrayList(stringIterable);

现在使用静态导入重写此代码,它将如下所示:

// store this function somewhere as public static final - it can be easily reused in the future
public static final Function<Map<String, Object>, Collection<String>> mapToKeysFunction = new Function<Map<String, Object>, Collection<String>>() {

    @Override
    public Collection<String> apply(final Map<String, Object> map) {
        return map.keySet();
    }
};

// ...

final List<Map<String, Object>> list = ...;
final List<String> result = newArrayList(concat(transform(list, mapToKeysFunction)));

希望这会有所帮助......

答案 3 :(得分:1)

如果您的对象属于字符串列表,即List<Map<String, List<String>>>

然后,您可以使用JAVA 8的 stream api获取扁平的值列表。以下是示例代码段:

List<Map<String, Object>> listOfMap = YOUR_OBJECT

List<String> finalList = listOfMap.stream().map(map -> (List<String>) map.get(KEY))
    .flatMap(x -> x.stream()).distinct()
    .collect(Collectors.toList());

如果您想要List<Object>中的List<Map<String, Object>>,则下面是代码段:

List<Map<String, Object>> listOfMap = YOUR_OBJECT

List<Object> finalList = listOfMap.stream().map(map -> map.get(KEY))
    .collect(Collectors.toList());

答案 4 :(得分:0)

您可以尝试以下

    List<Map<String, Object>>  mapList=new ArrayList<>();
    List<String> list=new ArrayList<>();
    for(Map<String,Object> i:mapList){
          for(Map.Entry<String,Object> entry:i.entrySet()){
                  list.add(entry.getKey());
          }
    }

如果您想要值,您可以按照以下方式执行

     List<Map<String, Object>>  mapList=new ArrayList<>();
    List<String> list=new ArrayList<>();
    for(Map<String,Object> i:mapList){
          for(Map.Entry<String,Object> entry:i.entrySet()){
                  list.add(entry.getValue().toString());
          }
    }

答案 5 :(得分:0)

List<Map<String, Object>> listMap = (declared somewhere);
List<String> stringList = new ArrayList<String>();

for (Map<String, Object> m : listMap) {
    for (Map.Entry<String, Object> e : m.entrySet()) {
        stringList.add(e.getValue());
    }
}

答案 6 :(得分:0)

示例代码:

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

class ListCheck 
{
    public static void main(String[] args) 
    {

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


        Map<String,Object> integerMap = new HashMap<String,Object>();

        integerMap.put("one",new Integer(1));
        integerMap.put("two",new Integer(2));
        integerMap.put("two",new Integer(3));

        Map<String,Object> map1 = new HashMap<String,Object>();

        Map<String,Object> doubleMap = new HashMap<String,Object>();

        doubleMap.put("one",new Double(1.0));
        doubleMap.put("two",new Double(2.0));
        doubleMap.put("two",new Double(3.0));


        newList.add(integerMap);
        newList.add(doubleMap);
        //target list to which will have all the values of the map
        List<String> targetList = new ArrayList<String>(); 

        /*Code to iterate Map and add the value to the list */

        for (Map<String,Object> currentMap : newList) {
               //check for currentMap null check
                 for (Map.Entry<String,Object> curEntry:currentMap.entrySet()){ 
                    String s = (String) curEntry.getValue().toString();  //handle your case accordingly,with null check and other to stirng
                    targetList.add(s);
                 }
        }


        /*Testing the targetList */

        for (String s : targetList) {
                System.out.println(s);
         }


    }
}