Associative array/Iteration: Difference between revisions
→{{header|Java}}
(Jakt) |
|||
Line 1,812:
=={{header|Java}}==
<p>
See also, [https://rosettacode.org/wiki/Associative_array/Iteration#Java Java - Associative array/Iteration].
</p>
<p>
You can access the <kbd>key</kbd> and <kbd>value</kbd> pairs by using the <code>Map.entrySet</code> method,
which will return a <code>Map.Entry</code>.<br />
It's worth noting that a <code>Map.Entry</code> also has the <code>setValue</code> method.
for (Map.Entry<String, Integer> e : map.entrySet()) {▼
</p>
String key = e.getKey();▼
System.out.println("key = " + key + ", value = " + value);▼
</syntaxhighlight>
<p>
You can access just the <kbd>key</kbd>s by using the <code>Map.keySet</code> method, which will return a <code>Set</code>.
for (String key : map.keySet()) {▼
</p>
▲ System.out.println("key = " + key);
<syntaxhighlight lang="java">
</syntaxhighlight>
<p>
▲ System.out.println("value = " + value);
And you can access just the <kbd>value</kbd>s by using the <code>Map.values</code> method, which will return a <code>Collection</code>.
▲}</syntaxhighlight>
</p>
<syntaxhighlight lang="java">
</syntaxhighlight>
<br />
Java 8 version
|