Java List Stream to Map using the Collectors toMap API : Java 8 Streams
Vložit
- čas přidán 27. 07. 2024
- In this Java Streams Tutorial, we take a Java List stream it to map using the collectors toMap API. We should you how to stream a list to hashmap and other maps such as stream a list to treemap. We discuss how to handle duplicate keys and cover all of the API properties of Collectors.toMap.
0:00 Introduction
1:30 Streaming List to HashMap
6:00 Handling Key Clashes Converting List to Map
10:45 Streaming List to a LinkedHashMap
Playlists:
HTML: • HTML Tutorial for Begi...
JavaScript: • JavaScript Building Bl...
CSS: • CSS Tutorial for Begin...
Java: • Java Tutorials for Beg...
It was really helpful, thank you man, thanks for the good work
Thank you the tutorial Is so helpful and concise
thanks for watching and feedback. glad you liked it.
Thanks. This is really useful for me.
Clear and concise demo!
Thank you saved my day
This helped, thanks mate!
Glad to hear that Adarsh, thanks for watching.
That was a good video 👍
thank you, glad you liked it
Thanks a million for the tutorial!
However, the exception really got me baffled...
Normally when a duplicate key is put in a map, it doesn't throw one, simple overwriting takes place instead...
So, are there any other substantial differences between using a plain map and manipulating it in a stream?
Hi, Shane thank you for the great explanation. Is there a way where I can apply a function on my list elements and store the list element and function return value in a key-value pair like a map?
Suppose I have a String List
List Ids = [id1, ids2, id3];
and a Function
public List getDays(String id) {
makes api call and fetch all the days in a list where id is present
return list;
}
Now I want to perform getDays function on each of the list elements of IDs and store it in a map
Map
Lalit - This snipplet will work below, I will have to contemplate to determine if there is a better way but here is your request with "off the top of my head" solution... I am not sure I would recommend something like this.. sometimes using streams in this fashion has the opposite affect of what it should have, clean easy to interpret readable code... In one sec, I will post a better way to do this.. this is not pretty here.
Example 1: Does exactly what you asked but is very ugly code. See example 2
public class MultipleListsToMap {
public static void main(String[] args) {
List myIds = Arrays.asList("id1", "id2", "id3");
Map myMap = myIds.stream().map(id -> {
Object[] myReturn = {id, retrieveFakeList(id)};
return myReturn;
}).collect(Collectors
.toMap(myArray -> (String) ((Object[]) myArray)[0],
myArray -> (List) ((Object[]) myArray)[1]));
System.out.println(myMap);
}
private static List retrieveFakeList(String id) {
return Arrays.asList(id,id,id);
}
}
Example 2: Better than Example 1
This produces the same result as example 1, the one that does it purely with Stream.map etc. This example 2 is much more readable and better code. Dont always have to use Streams.
public class MultipleListsToMap {
public static void main(String[] args) {
List myIds = Arrays.asList("id1", "id2", "id3");
final Map myMap = new HashMap();
myIds.forEach(id -> {
myMap.put(id,retrieveFakeList(id));
});
System.out.println(myMap);
}
private static List retrieveFakeList(String id) {
return Arrays.asList(id,id,id);
}
}
Sometimes you just dont have to use Streams, dont use streams for the sake of using Streams, use what is best for readable code and that meets your requirements. This example is clean and very easy to ready. No Streams. Example 2 and this example, Example 3, are better, cleaner code than what you requested with Example 1. Hope this helps.
Example 3:
public class MultipleListsToMap {
public static void main(String[] args) {
List myIds = Arrays.asList("id1", "id2", "id3");
Map myMap = new HashMap();
for(String id: myIds){
myMap.put(id, retrieveFakeList(id));
}
System.out.println(myMap);
}
private static List retrieveFakeList(String id) {
return Arrays.asList(id,id,id);
}
}
@@ShaneCrouch Thanks Shane, I totally agree with you that sometimes the answer could be very simple. I am a beginner to streams so just wanted to explore more. Thanks for all your examples.
Just wanted to share the below code snippet. I am not sure whether it is very efficient or not in comparison to yours (which is so straight forward, I don't why I didn't think of that
for(String id: myIds){
myMap.put(id, retrieveFakeList(id));
}
My code:
public class MultipleListsToMap {
public static void main(String[] args) {
List myIds = Arrays.asList("id1", "id2", "id3");
Map myMap = myIds.stream()
.collect(Collectors.toMap(id->id, id -> retrieveFakeList(id)));
private static List retrieveFakeList(String id) {
return Arrays.asList(id,id,id);
}
}
nice, better than the example I had given you.. excellent work
What’s the function.identify(), is it build in function in Java?
When in doubt, Java API docs are very good.
docs.oracle.com/javase/8/docs/api/java/util/function/Function.html