Java 将 List<Map<String, Object>> 转换为 List<String>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19833889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 20:34:28  来源:igfitidea点击:

Convert List<Map<String, Object>> to List<String>

java

提问by Akshar A K

I have a List of Map List<Map<String, Object>>.I need only the values of the List of Map to be moved into List<String>.

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

Can someone please let me know how to convert?

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

回答by Ruchira Gayan Ranaweera

You can try as follows

你可以尝试如下

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

If you want values you can do as follows

如果你想要值,你可以做如下

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

回答by Choc13

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

回答by Shamse Alam

Try this

尝试这个

   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
   }

回答by Prabhakaran Ramaswamy

You can do like this

你可以这样做

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

回答by Yuriy Nakonechnyy

I suggest you to use Guavalibraries from Google and do it functionally:

我建议你使用谷歌的番石榴库并在功能上做到这一点:

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);

now rewriting this code using static imports, it'll look like the following:

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

// 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)));

Hope this helps...

希望这可以帮助...

回答by Raghu

Sample Code:

示例代码:

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


    }
}

回答by Tarang Bhalodia

If your object is of type List of string, i.e List<Map<String, List<String>>>

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

Then you can get flattened list of values using streamapi of JAVA 8. Below is sample code snippet:

然后,您可以使用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());

And if you want List<Object>from List<Map<String, Object>>, then below is code snippet:

如果你想要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());