Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The issue here is that <a href="http://java.sun.com/javase/6/docs/api/java/util/Map.html" rel="noreferrer"><code>Map</code></a> has two values (a key and value), while a <a href="http://java.sun.com/javase/6/docs/api/java/util/List.html" rel="noreferrer"><code>List</code></a> only has one value (an element).</p> <p>Therefore, the best that can be done is to either get a <code>List</code> of the keys or the values. (Unless we make a wrapper to hold on to the key/value pair).</p> <p>Say we have a <code>Map</code>:</p> <pre><code>Map&lt;String, String&gt; m = new HashMap&lt;String, String&gt;(); m.put("Hello", "World"); m.put("Apple", "3.14"); m.put("Another", "Element"); </code></pre> <p>The keys as a <code>List</code> can be obtained by creating a new <a href="http://java.sun.com/javase/6/docs/api/java/util/ArrayList.html" rel="noreferrer"><code>ArrayList</code></a> from a <a href="http://java.sun.com/javase/6/docs/api/java/util/Set.html" rel="noreferrer"><code>Set</code></a> returned by the <a href="http://java.sun.com/javase/6/docs/api/java/util/Map.html#keySet()" rel="noreferrer"><code>Map.keySet</code></a> method:</p> <pre><code>List&lt;String&gt; list = new ArrayList&lt;String&gt;(m.keySet()); </code></pre> <p>While the values as a <code>List</code> can be obtained creating a new <code>ArrayList</code> from a <a href="http://java.sun.com/javase/6/docs/api/java/util/Collection.html" rel="noreferrer"><code>Collection</code></a> returned by the <a href="http://java.sun.com/javase/6/docs/api/java/util/Map.html#values()" rel="noreferrer"><code>Map.values</code></a> method:</p> <pre><code>List&lt;String&gt; list = new ArrayList&lt;String&gt;(m.values()); </code></pre> <p>The result of getting the <code>List</code> of keys:</p> <pre> Apple Another Hello </pre> <p>The result of getting the <code>List</code> of values:</p> <pre> 3.14 Element World </pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload