How to Convert a Map to List in Java

How do I convert a Map to List in Java?

List<Value> list = new ArrayList<Value>(map.values());

assuming:

Map<Key,Value> map;

Converting a Map with List values into a List in Java

I notice, that here you are directly fetching the list and appending the value to it, and storing it in the list.

After populating the map, you can use the following code to generate the result that you expect.

// After populating map
List<String> result = mapToPopulate.entrySet()
.stream()
.flatMap(en -> en.getValue().stream().map(v -> v + " " + en.getKey()))
.collect(Collectors.toList());

How to convert Values in a Hashmap to a List String

Use addAll instead of add, in order to add all the Strings of all the List<String>s to a single List<String> :

for (List<String> value : adminErrorMap.values())   
{
adminValues.addAll(value);
}

Convert a map of lists into a list of maps

You can first iterate through map entries and represent list elements as Map<String,Integer> and get a stream of lists of maps, and then reduce this stream to a single list.

Try it online!

Map<String, List<Integer>> mapOfLists = new LinkedHashMap<>();
mapOfLists.put("a", List.of(1, 2, 3));
mapOfLists.put("b", List.of(4, 5, 6));
mapOfLists.put("c", List.of(7));
List<Map<String, Integer>> listOfMaps = mapOfLists.entrySet().stream()
// Stream<List<Map<String,Integer>>>
.map(entry -> entry.getValue().stream()
// represent list elements as Map<String,Integer>
.map(element -> Map.of(entry.getKey(), element))
// collect a list of maps
.collect(Collectors.toList()))
// intermediate output
//[{a=1}, {a=2}, {a=3}]
//[{b=4}, {b=5}, {b=6}]
//[{c=7}]
.peek(System.out::println)
// reduce a stream of lists to a single list
// by sequentially multiplying the list pairs
.reduce((list1, list2) -> list1.stream()
// combinations of elements,
// i.e. maps, from two lists
.flatMap(map1 -> list2.stream()
.map(map2 -> {
// join entries of two maps
Map<String, Integer> map =
new LinkedHashMap<>();
map.putAll(map1);
map.putAll(map2);
return map;
}))
// collect into a single list
.collect(Collectors.toList()))
.orElse(null);
// output
listOfMaps.forEach(System.out::println);
//{a=1, b=4, c=7}
//{a=1, b=5, c=7}
//{a=1, b=6, c=7}
//{a=2, b=4, c=7}
//{a=2, b=5, c=7}
//{a=2, b=6, c=7}
//{a=3, b=4, c=7}
//{a=3, b=5, c=7}
//{a=3, b=6, c=7}

See also:

• Generate all possible string combinations by replacing the hidden # number sign

• How to get all possible combinations from two arrays?

How to convert a Map to Arraylist of Objects?

If you can expand your class to have a constructor taking the values as well:

map.entrySet()
.stream()
.map(e -> new PojoObject(e.getKey(), e.getValue()))
.collect(Collectors.toList());

If you can't:

map.entrySet()
.stream()
.map(e -> {
PojoObject po = new PojoObject();
po.setMapKey(e.getKey());
po.setMapValue(e.getValue());
return po;
}).collect(Collectors.toList());

Note that this uses Java 8 Stream API.

Converting a map of map to list of map in java

How about something like this?

ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
dateTeamCountMap.forEach((date, nestedMap) -> {
Map<String, String> temp = new HashMap<String, String>();
temp.put("Date",date);
nestedMap.forEach((team, count) -> {
temp.put(team,count);
});
list.add(temp);
});

If you have a class representing {Date:28-09-2018, India:14, Australia:26} then you can create List<YourClass> too.

How does one convert a HashMap to a List in Java?

HashMap<Integer, String> map = new HashMap<Integer, String>();
map.put (1, "Mark");
map.put (2, "Tarryn");
List<String> list = new ArrayList<String>(map.values());
for (String s : list) {
System.out.println(s);
}

Convert Map to List using Java 8 Pattern matching

I'm guessing you meant streams from java-8 and the requirement, that the key must match "Address1Emp.*" regex.

You can use the following code:

map.entrySet().stream() // create a stream of entries
.filter(e -> e.getKey().matches("Address1Emp.*")) // leave only those entries, whose keys start with "Address1"
.map(Map.Entry::getValue) // get values only
.collect(Collectors.toList());

Edit after you have added the inner lists:

map.entrySet().stream() // create a stream of entries
.filter(e -> e.getKey().matches("Address1Emp.*")) // leave only those entries, whose keys start with "Address1"
.flatMap(e -> e.getValue().stream()) // get values only
.collect(Collectors.toList());

Convert Map K, List V to Map K, V where V have 2 lists of objects

What you've got looks pretty good already. Just replace the value mapper with e -> convert(e.getValue()) where convert is like the function below that converts a List<ObjectB> into an ObjectC:

ObjectC convert(List<ObjectB> list) {
ObjectC c = new ObjectC();
for (ObjectB b : list) {
c.ba.add(b.ba);
c.bb.add(b.bb);
}
return c;
}

Or if you'd prefer to stick with just using streams, try Stream#collect like so:

e.getValue().stream().collect(
() -> new ObjectC(),
(c, b) -> {
c.ba.add(b.ba);
c.bb.add(b.bb);
},
(c1, c2) -> {
c1.ba.addAll(c2.ba);
c1.bb.addAll(c2.bb);
}
)


Related Topics



Leave a reply



Submit