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); } }