Тёмный

Java List Stream to Map using the Collectors toMap API : Java 8 Streams 

Shane Crouch - The Coding Zoo
Подписаться 4,7 тыс.
Просмотров 8 тыс.
50% 1

Опубликовано:

 

14 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 19   
@MuratKutluTuna
@MuratKutluTuna 10 месяцев назад
It was really helpful, thank you man, thanks for the good work
@haurlim1804
@haurlim1804 Год назад
Thanks. This is really useful for me.
@abhishekomprakash4074
@abhishekomprakash4074 Год назад
Clear and concise demo!
@okunnigasamson3244
@okunnigasamson3244 4 года назад
Thank you the tutorial Is so helpful and concise
@ShaneCrouch
@ShaneCrouch 4 года назад
thanks for watching and feedback. glad you liked it.
@abhisheksarangi3844
@abhisheksarangi3844 3 года назад
Thank you saved my day
@OriginalLany
@OriginalLany 3 года назад
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?
@adarsha6862
@adarsha6862 4 года назад
This helped, thanks mate!
@ShaneCrouch
@ShaneCrouch 4 года назад
Glad to hear that Adarsh, thanks for watching.
@pralanvideos9631
@pralanvideos9631 3 года назад
That was a good video 👍
@ShaneCrouch
@ShaneCrouch 3 года назад
thank you, glad you liked it
@lalitverma6677
@lalitverma6677 3 года назад
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
@ShaneCrouch
@ShaneCrouch 3 года назад
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); } }
@ShaneCrouch
@ShaneCrouch 3 года назад
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); } }
@ShaneCrouch
@ShaneCrouch 3 года назад
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); } }
@lalitverma6677
@lalitverma6677 3 года назад
@@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); } }
@ShaneCrouch
@ShaneCrouch 3 года назад
nice, better than the example I had given you.. excellent work
@woonjeentang8755
@woonjeentang8755 3 года назад
What’s the function.identify(), is it build in function in Java?
@ShaneCrouch
@ShaneCrouch 3 года назад
When in doubt, Java API docs are very good. docs.oracle.com/javase/8/docs/api/java/util/function/Function.html
Далее
Optionals In Java - Simple Tutorial
15:53
Просмотров 215 тыс.
Flipping Robot vs Heavier And Heavier Objects
00:34
Просмотров 39 млн
Java 8 STREAMS Tutorial
18:10
Просмотров 741 тыс.
Java Streams: Beyond The Basics
49:21
Просмотров 72 тыс.
CompletableFuture: The Promises of Java
47:47
Просмотров 49 тыс.
Java Streams Tutorial | 2020
19:13
Просмотров 262 тыс.
Stream API in Java
26:04
Просмотров 338 тыс.