Associative array/Creation: Difference between revisions

(Jakt)
Line 3,488:
 
=={{header|Java}}==
<p>
{{works with|Java|1.5+}}
Java offers an associative array, referred to as a <code>Map</code>, as part of the Collections Framework.<br />
 
There are numerous implementing classes, each which provide unique functionality for various tasks.<br />
Defining the Map:
</p>
<syntaxhighlight lang="java5">Map<String, Integer> map = new HashMap<String, Integer>();
<p>
map.put("foo", 5);
It's worth noting that Java also offers the <code>Dictionary</code> class, which appears to be less preferred, according to Java.<br />
map.put("bar", 10);
<kbd><i>"[The Map] interface takes the place of the Dictionary class ..."</i></kbd>.
map.put("baz", 15);
</p>
map.put("foo", 6);</syntaxhighlight>
<p>
"Putting" a value for a key that already exists ("map.put("foo", 6)" in this example) will replace and return the old value for the key.
The most generalized <code>Map</code> would be the <code>HashMap</code>, which is a basic, unordered, set of <kbd>keys</kbd> and <kbd>values</kbd>.<br />
 
There is also the <code>LinkedHashMap</code>, which will preserve the order of input.<br />
Initializing a Map as a class member:
There is the <code>TreeMap</code>, which is used to store the <kbd>key</kbd>s in a specific order, using the <kbd>key</kbd>'s <code>compareTo</code> method.
<syntaxhighlight lang="java5">public static Map<String, Integer> map = new HashMap<String, Integer>(){{
Optionally, you could provide your own <kbd>comparator</kbd> using the <code>Comparator</code> interface, which I'll demonstrate below.<br />
put("foo", 5);
There are numerous other implementing classes, which can be found under the <code>Map</code> <kbd>Javadoc</kbd>,
put("bar", 10);
[https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/util/Map.html. Map (Java SE 20 & JDK 20)].
put("baz", 15);
</p>
put("foo", 6);
<p>
}};</syntaxhighlight>
Here is a basic implementation of a <code>HashMap</code>.
Retrieving a value:
</p>
<syntaxhighlight lang="java5">map.get("foo"); // => 6
<syntaxhighlight lang="java">
map.get("invalid"); // => null</syntaxhighlight>
Map<String, Integer> map = new HashMap<>();
Note that it is possible to put <code>null</code> as a value, so <code>null</code> being returned by <code>get</code> is not sufficient for determining that the key is not in the <code>Map</code>. There is a <code>containsKey</code> method for that.
</syntaxhighlight>
 
<p>
Iterate over keys:
To add a <kbd>key</kbd> and <kbd>value</kbd> pair, you use the <code>Map.put</code> method.<br />
<syntaxhighlight lang="java5">for (String key: map.keySet())
A useful feature of <code>Map.put</code> is that if the <kbd>key</kbd> already exists, thus is being overridden,
System.out.println(key);</syntaxhighlight>
it will return the overridden value, otherwise <code>null</code>.
Iterate over values:
</p>
<syntaxhighlight lang="java5">for (int value: map.values())
System.out.println(value);</syntaxhighlight lang="java">
map.put("rosetta", 100);
Iterate over key, value pairs:
map.put("code", 200);
<syntaxhighlight lang="java5">for (Map.Entry<String, Integer> entry: map.entrySet())
System.out.println(entry.getKey() + " => " + entry.getValue());</syntaxhighlight>
<p>
To get a <kbd>value</kbd>, you use the <code>Map.get</code> method, specifying the <kbd>key</kbd> as the parameter.<br />
If the specified <kbd>key</kbd> does not exist, <code>null</code> is returned.
</p>
<syntaxhighlight lang="java">
int valueA = map.get("rosetta");
int valueB = map.get("code");
</syntaxhighlight>
<p>
To mutate a <kbd>key</kbd>'s <kbd>value</kbd>, you use the <code>Map.replace</code> method.
</p>
<syntaxhighlight lang="java">
map.replace("rosetta", 300);
</syntaxhighlight>
<p>
Alternately, you can replace the <kbd>value</kbd>, only if it is of a current <kbd>value</kbd>.<br />
So, in this example it will return <kbd>true</kbd>, only if the <kbd>key</kbd> <kbd><i>"rosetta"</i></kbd> had the current <kbd>value</kbd> of <kbd><i>100</i></kbd>.
</p>
<syntaxhighlight lang="java">
boolean replaced = map.replace("rosetta", 100, 300);
</syntaxhighlight>
<p>
To check for the existence of a <kbd>key</kbd>, you use the <code>containsKey</code> method.
</p>
<syntaxhighlight lang="java">
boolean contains = map.containsKey("rosetta");
</syntaxhighlight>
<p>
And to check for the existence of a <kbd>value</kbd>, you use the <code>containsValue</code> method.
</p>
<syntaxhighlight lang="java">
boolean contains = map.containsValue(100);
</syntaxhighlight>
<p>
A <code>LinkedHashMap</code> is exactly the same as a <code>HashMap</code>, except it will preserve the order in which the <kbd>key</kbd>s were added.
</p>
<syntaxhighlight lang="java">
Map<String, Integer> map = new LinkedHashMap<>();
map.put("rosetta", 100);
map.put("code", 200);
</syntaxhighlight>
<p>
A <code>TreeMap</code> is useful for when you want the <kbd>key</kbd>s in a specific order.<br />
By default, it will use the <kbd>key</kbd>'s <code>compareTo</code> method, to determine the order.<br />
So, if you're <kbd>key</kbd> is a <code>String</code>, the order will be ascending, similar to an actual dictionary.
</p>
<syntaxhighlight lang="java">
Map<String, Integer> map = new TreeMap<>();
map.put("rosetta", 100);
map.put("code", 200);
</syntaxhighlight>
<p>
You could, optionally, specify a comparator by implementing a <code>Comparator</code> interface.<br />
A <code>TreeMap</code>, conveniently, only requires you to implement the <code>compare</code> method of the <code>Comparator</code>,
so the implementation can be done as an <kbd>anonymous class</kbd>.
</p>
<syntaxhighlight lang="java">
Comparator<String> comparator = new Comparator<String>() {
public int compare(String stringA, String stringB) {
if (stringA.compareTo(stringB) > 0) {
return -1;
} else if (stringA.compareTo(stringB) < 0) {
return 1;
}
return 0;
}
};
</syntaxhighlight>
<p>
Which you then supply as an argument to the constructor.
</p>
<syntaxhighlight lang="java">
Map<String, Integer> map = new TreeMap<>(comparator);
</syntaxhighlight>
<p>
To make things even simpler, you could use a <kbd>lambda</kbd> for the <kbd>anonymous class</kbd>.
</p>
<syntaxhighlight lang="java">
Comparator<String> comparator = (stringA, stringB) -> {
if (stringA.compareTo(stringB) > 0) {
return -1;
} else if (stringA.compareTo(stringB) < 0) {
return 1;
}
return 0;
};
</syntaxhighlight>
 
=={{header|JavaScript}}==
118

edits