fork download
  1. import java.util.Collection;
  2. import java.util.Collections;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.stream.Collectors;
  7.  
  8. class Ideone {
  9. public static void main(String[] args) {
  10. final Map<Object, Integer> map = new HashMap<>();
  11. map.put("foo", 3);
  12. map.put("bar", 2);
  13. map.put(42, 5);
  14. System.out.println(transform(map));
  15. }
  16.  
  17. private static <T> List<T> transform(Map<? extends T, Integer> map) {
  18. return map.entrySet().stream()
  19. .map(entry -> Collections.nCopies(entry.getValue(), entry.getKey()))
  20. .flatMap(Collection::stream)
  21. .collect(Collectors.toList());
  22. }
  23. }
Success #stdin #stdout 0.1s 50552KB
stdin
Standard input is empty
stdout
[bar, bar, foo, foo, foo, 42, 42, 42, 42, 42]