import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
class Ideone {
public static void main
(String[] args
) { final Map
<Object, Integer
> map
= new HashMap
<>(); map.put("foo", 3);
map.put("bar", 2);
map.put(42, 5);
System.
out.
println(transform
(map
)); }
private static <T> List<T> transform(Map<? extends T, Integer> map) {
return map.entrySet().stream()
.
map(entry
-> Collections.
nCopies(entry.
getValue(), entry.
getKey())) .collect(Collectors.toList());
}
}
aW1wb3J0IGphdmEudXRpbC5Db2xsZWN0aW9uOwppbXBvcnQgamF2YS51dGlsLkNvbGxlY3Rpb25zOwppbXBvcnQgamF2YS51dGlsLkhhc2hNYXA7CmltcG9ydCBqYXZhLnV0aWwuTGlzdDsKaW1wb3J0IGphdmEudXRpbC5NYXA7CmltcG9ydCBqYXZhLnV0aWwuc3RyZWFtLkNvbGxlY3RvcnM7CgpjbGFzcyBJZGVvbmUgewogIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgIGZpbmFsIE1hcDxPYmplY3QsIEludGVnZXI+IG1hcCA9IG5ldyBIYXNoTWFwPD4oKTsKICAgIG1hcC5wdXQoImZvbyIsIDMpOwogICAgbWFwLnB1dCgiYmFyIiwgMik7CiAgICBtYXAucHV0KDQyLCA1KTsKICAgIFN5c3RlbS5vdXQucHJpbnRsbih0cmFuc2Zvcm0obWFwKSk7CiAgfQoKICBwcml2YXRlIHN0YXRpYyA8VD4gTGlzdDxUPiB0cmFuc2Zvcm0oTWFwPD8gZXh0ZW5kcyBULCBJbnRlZ2VyPiBtYXApIHsKICAgIHJldHVybiBtYXAuZW50cnlTZXQoKS5zdHJlYW0oKQogICAgICAgIC5tYXAoZW50cnkgLT4gQ29sbGVjdGlvbnMubkNvcGllcyhlbnRyeS5nZXRWYWx1ZSgpLCBlbnRyeS5nZXRLZXkoKSkpCiAgICAgICAgLmZsYXRNYXAoQ29sbGVjdGlvbjo6c3RyZWFtKQogICAgICAgIC5jb2xsZWN0KENvbGxlY3RvcnMudG9MaXN0KCkpOwogIH0KfQ==
[bar, bar, foo, foo, foo, 42, 42, 42, 42, 42]