Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine/merge multiple maps into 1 map

Tags:

dart

How can I combine/merge 2 or more maps in dart into 1 map? for example I have something like:

 var firstMap = {"1":"2"};  var secondMap = {"1":"2"};  var thirdMap = {"1":"2"}; 

I want:

 var finalMap = {"1":"2", "1":"2", "1":"2"}; 
like image 676
smriti Avatar asked Jul 19 '17 20:07

smriti


People also ask

How do I put multiple maps on one map?

concat() Alternatively, we can use Stream#concat() function to merge the maps together. This function can combine two different streams into one. As shown in the snippet, we are passed the streams of map1 and map2 to the concate() function and then collected the stream of their combined entry elements.

Can you combine maps in Google Maps?

If you want to combine two My Maps custom maps: Go to first map and select "Add a New Layer" Go to the second map and in the top menu ( three dots) and choose Export to kml-->save the file to your computer. Go to first map, click into the new layer and choose Import--> import the kml file you previously saved.

Can you combine 2 Minecraft maps?

You can't merge maps. The only way to fully explore a map is to manually explore all the terrain shown on the map while holding that map.

How do you do a joint map?

As we know hashmap does not allow duplicate keys. So when we merge the maps in this way, for duplicate keys in firstMap the value is overwritten by the value for the same key in secondMap . Let's take an example. //map 1 HashMap<Integer, String> firstMap = new HashMap<>(); firstMap.


1 Answers

you can use addAll method of Map object

var firstMap = {"1":"2"}; var secondMap = {"2":"3"};  var thirdMap = {};  thirdMap.addAll(firstMap); thirdMap.addAll(secondMap);  print(thirdMap); 

Or

var thirdMap = {}..addAll(firstMap)..addAll(secondMap); 

Update

Since dart sdk 2.3 You can use spread operator ...

final firstMap = {"1":"2"}; final secondMap = {"2":"3"};  final thirdMap = {    ...firstMap,    ...secondMap, }; 
like image 185
Hadrien Lejard Avatar answered Oct 02 '22 15:10

Hadrien Lejard