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

Sdílet
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...

Komentáře • 19

  • @MuratKutluTuna
    @MuratKutluTuna Před 7 měsíci +1

    It was really helpful, thank you man, thanks for the good work

  • @okunnigasamson3244
    @okunnigasamson3244 Před 3 lety +5

    Thank you the tutorial Is so helpful and concise

    • @ShaneCrouch
      @ShaneCrouch  Před 3 lety

      thanks for watching and feedback. glad you liked it.

  • @haurlim1804
    @haurlim1804 Před rokem

    Thanks. This is really useful for me.

  • @abhishekomprakash4074
    @abhishekomprakash4074 Před 9 měsíci

    Clear and concise demo!

  • @abhisheksarangi3844
    @abhisheksarangi3844 Před 3 lety

    Thank you saved my day

  • @adarsha6862
    @adarsha6862 Před 3 lety +2

    This helped, thanks mate!

    • @ShaneCrouch
      @ShaneCrouch  Před 3 lety

      Glad to hear that Adarsh, thanks for watching.

  • @pralanvideos9631
    @pralanvideos9631 Před 3 lety +3

    That was a good video 👍

  • @OriginalLany
    @OriginalLany Před 2 lety +2

    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?

  • @lalitverma6677
    @lalitverma6677 Před 3 lety +1

    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  Před 3 lety +1

      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  Před 3 lety +1

      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  Před 3 lety +1

      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 Před 3 lety +1

      @@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  Před 3 lety +1

      nice, better than the example I had given you.. excellent work

  • @woonjeentang8755
    @woonjeentang8755 Před 3 lety

    What’s the function.identify(), is it build in function in Java?

    • @ShaneCrouch
      @ShaneCrouch  Před 3 lety

      When in doubt, Java API docs are very good.
      docs.oracle.com/javase/8/docs/api/java/util/function/Function.html