How to convert ArrayList to HashMap or LinkedHashMap in Java 8 - Example Tutorial | Java67
1) You can use the Function.identity() function if you are passing the object itself in the lambda expression. For example, lambda expression s -> s can be replaced with Function.identity() call.2) Use the static important feature to import static methods of Collectors e.g. toMap(), this will simplify your code.
3) The toMap(keyExtractor, valueExtractor) doesn't provide any guarantee of what kind of map it will return.
4) If your List contains duplicate elements and you are using them as the key then you should use toMap(keyMapper, valueMapper, mergeFunction). The merge function used to resolve collisions between values associated with the same key, as supplied to Map.merge(Object, Object, BiFunction). See Java SE 8 for Really impatient to learn more about merge() function of Map interface in Java 8.
Read full article from How to convert ArrayList to HashMap or LinkedHashMap in Java 8 - Example Tutorial | Java67
No comments:
Post a Comment