How to convert a stream to a HashMap (not Map) in Java 8

alexgbelov

I have a stream, and I want to explicitly collect it into a HashMap. Right now, I'm doing something like

List<Item> listToTransform = //initialized and populated
HashMap<K, V> myHashMap = new HashMap<>();
listToTransform.stream().map(/* do my transformation here */)
.forEach(i -> myHashMap.put(i.getKey(), i.getValue()));

I was wondering if there was a way of using Collectors to explicitly get back a HashMap.

Joe C

One of the overloads to the Collectors.toMap method will allow you to select a map implementation of your choice.

Unfortunately, one of the drawbacks to this overload is that it also requires a method to merge two values when they have the same key (though I often reference a method that always throws in that case).

HashMap<K, V> myHashMap = listToTransform.stream().map(/* do my transformation here */)
.collect(Item::getKey, Item::getValue, this::throwIllegalArgumentException, HashMap::new);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Convert stream of map to HashMap in Java

From Java

How to convert Array to HashMap using Java 8 Stream

From Java

How to convert List to Map with indexes using stream - Java 8?

From Dev

How to convert Array to HashMap in Java with Stream API

From Java

Java 8 Convert a stream of set to Map

From Java

java 8 stream convert complex map

From Dev

Java 8 stream: how to Convert Map<String, List<Integer>> to Map<Integer, List<String>> using groupingBy(.)

From Dev

Java8: How to Convert Map<X, List<Y>> to Map<Y,X> using Stream?

From Dev

Java 8 stream map custom function and convert it to Map

From Java

Java Stream API: how to convert to a Map of Sets

From Dev

How to convert List<AnyClassObject> to List<Map<String, String>> using Stream in java 8?

From Java

Java 8 Stream API: how to convert a List to a Map<Long, Set> having repeated keys in the List?

From Java

Java 8 | Parallel Stream for a HashMap

From Java

How to convert a for iteration with conditions to Java 8 stream

From Java

How to convert a Java 8 Stream to an Array?

From Java

How to convert a String to a Java 8 Stream of Characters?

From Java

How to convert Stream of Characters into a String in Java 8

From Java

Java8: HashMap<X, Y> to HashMap<X, Z> using Stream / Map-Reduce / Collector

From Dev

How to convert nested for loop into Hashmap using java stream

From Java

Java8: convert one map to an another using stream

From Java

Java 8 - stream convert map's value type

From Dev

Convert GraphTraversal<Vertex, Map<Object, List>> into Java8 stream

From Dev

Convert map to specific dto object using stream JAVA 8

From Dev

Convert list of Object to Map<Key, List<Object>> with java 8 stream

From Dev

How to filter Hashmap to update the value on the fly using Java 8 stream?

From Java

How can I stream a Map of String and Map using java 8?

From Dev

How to combine Map values from parent Map with java 8 stream

From Dev

How to convert a list of objects into a map in Java 8?

From Java

How to convert Map to List in Java 8

Related Related

  1. 1

    Convert stream of map to HashMap in Java

  2. 2

    How to convert Array to HashMap using Java 8 Stream

  3. 3

    How to convert List to Map with indexes using stream - Java 8?

  4. 4

    How to convert Array to HashMap in Java with Stream API

  5. 5

    Java 8 Convert a stream of set to Map

  6. 6

    java 8 stream convert complex map

  7. 7

    Java 8 stream: how to Convert Map<String, List<Integer>> to Map<Integer, List<String>> using groupingBy(.)

  8. 8

    Java8: How to Convert Map<X, List<Y>> to Map<Y,X> using Stream?

  9. 9

    Java 8 stream map custom function and convert it to Map

  10. 10

    Java Stream API: how to convert to a Map of Sets

  11. 11

    How to convert List<AnyClassObject> to List<Map<String, String>> using Stream in java 8?

  12. 12

    Java 8 Stream API: how to convert a List to a Map<Long, Set> having repeated keys in the List?

  13. 13

    Java 8 | Parallel Stream for a HashMap

  14. 14

    How to convert a for iteration with conditions to Java 8 stream

  15. 15

    How to convert a Java 8 Stream to an Array?

  16. 16

    How to convert a String to a Java 8 Stream of Characters?

  17. 17

    How to convert Stream of Characters into a String in Java 8

  18. 18

    Java8: HashMap<X, Y> to HashMap<X, Z> using Stream / Map-Reduce / Collector

  19. 19

    How to convert nested for loop into Hashmap using java stream

  20. 20

    Java8: convert one map to an another using stream

  21. 21

    Java 8 - stream convert map's value type

  22. 22

    Convert GraphTraversal<Vertex, Map<Object, List>> into Java8 stream

  23. 23

    Convert map to specific dto object using stream JAVA 8

  24. 24

    Convert list of Object to Map<Key, List<Object>> with java 8 stream

  25. 25

    How to filter Hashmap to update the value on the fly using Java 8 stream?

  26. 26

    How can I stream a Map of String and Map using java 8?

  27. 27

    How to combine Map values from parent Map with java 8 stream

  28. 28

    How to convert a list of objects into a map in Java 8?

  29. 29

    How to convert Map to List in Java 8

HotTag

Archive