2

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>.

Can someone please let me know how to convert?

5
  • 1
    Can you put up your working/non-working code ? Commented Nov 7, 2013 at 10:47
  • Please format your post correctly, clarify your problem and show us what you've tried so far. Commented Nov 7, 2013 at 10:47
  • 3
    Create a new list, loop over the list of maps, for each map add the values to your new list. Please try it yourself and post your code if you get stuck. Commented Nov 7, 2013 at 10:48
  • The values of the map Map<String, Object> are of type Object. You want to convert these VALUES to type String and store them in a list? Correct? Or is it the KEYS of type String you want to store in a separate list? Commented Nov 7, 2013 at 10:49
  • I have similar problem where I need Objects in Map to be stored in a List. I would like to know a solution using Java Streams API. Commented Jun 11, 2020 at 5:46

7 Answers 7

2

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());
      }
Sign up to request clarification or add additional context in comments.

3 Comments

Quote OP: "... to be moved into List<String>".
@MarkoTopolnik: ByteCode's right. Quote: I need only the values of the List of Map [i.e. the objects] to be moved into List<String>.
@W.K.S And how is ByteCode right when he moves the objects into a List<Object>? And btw the objects themselves are not strings.
1

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
   }

Comments

1

I suggest you to use Guava libraries 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...

Comments

1

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

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

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<Map<String, Object>> listOfMap = YOUR_OBJECT

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

3 Comments

Please answer according to question and not create your own scenario.
May be not related to the question. But this answer helped me in another way.
What is KEY in map.get(KEY) ?
0

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

4 Comments

Why not let him try on his own first ?
@AnkitRustagi ... to get easy karma?
Karma is a myth PS- i am from India ;)
@AnkitRustagi I figured as much---sorry about the Geek Culture misusing your native myth for something much more mundane :)
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());
    }
}

Comments

0

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


    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.