Extending on @JonSkeet's answer, I would suggest that you use Guava's ListMultimap
as your (A) type. This type makes sure that you always have lists with at least one element as part of the map, and the .asMap()
method returns a view that is a Map<String, List<String>>
(even if the signature does not say so).
The Maps.transformValues
method also returns a view, so changes in the source map are reflected in the target map. Be aware, however, that "The returned map is not thread-safe or serializable, even if the underlying map is.":
ListMultimap<String, String> source = ArrayListMultimap.create();
Map<String, String> target = Maps.transformValues(source.asMap(), new Function<Collection<String>, String>() {
@Override
public String apply(Collection<String> input) {
return Iterables.getLast(input);
}
});