How to convert Values in a Hashmap to a List<String>
Use addAll
instead of add
, in order to add all the String
s of all the List<String>
s to a single List<String>
:
for (List<String> value : adminErrorMap.values())
{
adminValues.addAll(value);
}
In Java8, you can use functional to do that:
adminErrorMap.values().forEach(adminValues::addAll);
You just need to flatten the collection. In Java8:
final Map<String, List<String>> adminErrorMap = ImmutableMap.of(
"a", Lists.newArrayList("first", "second"),
"b", Lists.newArrayList("third")
);
final List<String> results = adminErrorMap.values().stream()
.flatMap(Collection::stream)
.collect(Collectors.toList());
results.forEach(System.out::println);
It prints:
first
second
third