Associative array/Merging: Difference between revisions

m
Moved Vlang entry into correct alphabetical order.
m (BaCon moved to the BASIC section.)
m (Moved Vlang entry into correct alphabetical order.)
 
(3 intermediate revisions by 2 users not shown)
Line 771:
 
=={{header|Java}}==
<p>
<syntaxhighlight lang="java">import java.util.*;
See also, [https://rosettacode.org/wiki/Associative_array/Creation#Java Java - Associative array/Creation].
</p>
<p>
This task illustrates the difference between statically typed languages, and dynamically typed.<br />
With exception to the <code>var</code> keyword, Java is statically typed.
So, using an undefined, non-static, <kbd>value</kbd>, creates a trivial situation.
</p>
<p>
A good way to understand this task is that it's not possible in a static-typed language.<br />
It defeats the purpose of defining the data-type.<br />
For Java, if you're going to use <code>Object</code> as your data-type, you need to re-evaluate the method of abstraction.
</p>
<p>
Java offers <kbd>generics</kbd>, <kbd>interfaces</kbd>, and <kbd>abstract classes</kbd> for this task.
</p>
<p>
Considering this, to complete this specific task, I would just store the values as <kbd>strings</kbd>.
</p>
<syntaxhighlight lang="java">
import java.util.LinkedHashMap;
import java.util.Map;
</syntaxhighlight>
<syntaxhighlight lang="java">
Map<String, String> mapA = new LinkedHashMap<>();
mapA.put("name", "Rocket Skates");
mapA.put("price", "12.75");
mapA.put("color", "yellow");
 
Map<String, String> mapB = new LinkedHashMap<>();
class MergeMaps {
mapB.put("price", "15.25");
public static void main(String[] args) {
mapB.put("color", "red");
Map<String, Object> base = new HashMap<>();
basemapB.put("nameyear", "Rocket Skates1974");
base.put("price", 12.75);
base.put("color", "yellow");
Map<String, Object> update = new HashMap<>();
update.put("price", 15.25);
update.put("color", "red");
update.put("year", 1974);
 
Map<String, ObjectString> resultmapC = new HashMapLinkedHashMap<>(base);
resultmapC.putAll(updatemapA);
mapC.putAll(mapB);
</syntaxhighlight>
To show that the original <kbd>map</kbd>s are not affected.
<syntaxhighlight lang="java">
for(Map.Entry<String, String> entry : mapA.entrySet())
System.out.printf("%-20s%s%n", entry.getKey(), entry.getValue());
 
for(Map.Entry<String, String> entry : mapB.entrySet())
System.out.println(result);
System.out.printf("%-20s%s%n", entry.getKey(), entry.getValue());
}
 
}</syntaxhighlight>
for(Map.Entry<String, String> entry : mapC.entrySet())
{{output}}
System.out.printf("%-20s%s%n", entry.getKey(), entry.getValue());
<pre>{name=Rocket Skates, color=red, year=1974, price=15.25}</pre>
</syntaxhighlight>
<pre>
name Rocket Skates
price 12.75
color yellow
</pre>
<pre>
price 15.25
color red
year 1974
</pre>
<pre>
name Rocket Skates
price 15.25
color red
year 1974
</pre>
<p>
While not recommended, due to scalability, if you did want to use an <code>Object</code> as the value, you could use the following implementation.<br />
This will produce the same output as above.
</p>
<syntaxhighlight lang="java">
Map<String, Object> mapA = new LinkedHashMap<>();
mapA.put("name", "Rocket Skates");
mapA.put("price", 12.75);
mapA.put("color", "yellow");
 
Map<String, Object> mapB = new LinkedHashMap<>();
mapB.put("price", 15.25);
mapB.put("color", "red");
mapB.put("year", 1974);
 
Map<String, Object> mapC = new LinkedHashMap<>();
mapC.putAll(mapA);
mapC.putAll(mapB);
</syntaxhighlight>
 
=={{header|JavaScript}}==
Line 1,853 ⟶ 1,916:
color red
year 1974
</pre>
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascript">var mergeMaps = Fn.new { |m1, m2|
var m3 = {}
for (key in m1.keys) m3[key] = m1[key]
for (key in m2.keys) m3[key] = m2[key]
return m3
}
 
var base = { "name": "Rocket Skates" , "price": 12.75, "color": "yellow" }
var update = { "price": 15.25, "color": "red", "year": 1974 }
var merged = mergeMaps.call(base, update)
System.print(merged)</syntaxhighlight>
 
{{out}}
<pre>
{name: Rocket Skates, color: red, price: 15.25, year: 1974}
</pre>
 
Line 1,905 ⟶ 1,950:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var mergeMaps = Fn.new { |m1, m2|
var m3 = {}
for (key in m1.keys) m3[key] = m1[key]
9,476

edits