2

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

有人可以告诉我如何转换吗?

4

7 回答 7

2

你可以这样做

      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());
      }
于 2013-11-07T10:55:47.737 回答
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
   }
于 2013-11-07T10:55:08.547 回答
1

我建议你使用谷歌的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)));

希望这可以帮助...

于 2013-11-07T10:55:52.197 回答
1

如果您的对象是字符串列表类型,即List<Map<String, List<String>>>

然后,您可以使用 JAVA 8 的流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>from List<Map<String, Object>>,那么下面是代码片段:

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

List<Object> finalList = listOfMap.stream().map(map -> map.get(KEY))
    .collect(Collectors.toList());
于 2017-08-30T04:47:07.713 回答
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());
    }
}
于 2013-11-07T10:53:09.787 回答
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());
          }
    }
于 2013-11-07T10:51:21.953 回答
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);
         }


    }
}
于 2013-11-07T11:12:56.470 回答