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?
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());
}
List<String>".List<Object>? And btw the objects themselves are not strings.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...
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());
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());
}
}
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);
}
}
}
Map<String, Object>are of typeObject. You want to convert these VALUES to typeStringand store them in a list? Correct? Or is it the KEYS of typeStringyou want to store in a separate list?