title: How can HashMap.keySet() return a view of keys?
body: <p>Here is the keySet() function inside java.util.HasMap class :</p>
  
  <pre><code>public Set&lt;K&gt; keySet() {
      Set&lt;K&gt; ks = keySet;
      if (ks == null) {
          ks = new KeySet();
          keySet = ks;
      }
      return ks;
  }
  </code></pre>
  
  <p>In the comment, it says this function</p>
  
  <blockquote>
    <p>Returns a {@link Set} view of the keys contained in this map.
    The set is backed by the map, so changes to the map are
    reflected in the set, and vice-versa.</p>
  </blockquote>
  
  <p>Therefore, I expected the object of KeySet type, which this functions returns would contain a reference to the "view of keys".
  However when I look at the code, the KeySet class does not contain any field at all, and all of its super classes either.</p>
  
  <pre><code>final class KeySet extends AbstractSet&lt;K&gt; {
      public final int size()                 { return size; }
      public final void clear()               { HashMap.this.clear(); }
      public final Iterator&lt;K&gt; iterator()     { return new KeyIterator(); }
      public final boolean contains(Object o) { return containsKey(o); }
      public final boolean remove(Object key) {
          return removeNode(hash(key), key, null, false, true) != null;
      }
      public final Spliterator&lt;K&gt; spliterator() {
          return new KeySpliterator&lt;&gt;(HashMap.this, 0, -1, 0, 0);
      }
      public final void forEach(Consumer&lt;? super K&gt; action) {
          Node&lt;K,V&gt;[] tab;
          if (action == null)
              throw new NullPointerException();
          if (size &gt; 0 &amp;&amp; (tab = table) != null) {
              int mc = modCount;
              for (int i = 0; i &lt; tab.length; ++i) {
                  for (Node&lt;K,V&gt; e = tab[i]; e != null; e = e.next)
                      action.accept(e.key);
              }
              if (modCount != mc)
                  throw new ConcurrentModificationException();
          }
      }
  }
  </code></pre>
  
  <p>Could somebody please explain </p>
  
  <ol>
  <li>What does "a view of the keys" mean? What kind of data is "a view"?</li>
  <li>How can the HashMap.keySet() returns a view of the keys by using KeySet object, which doesn't contain any reference to anything at all?</li>
  </ol>
  
  <p>To make it more clear : </p>
  
  <p>Here is a code sample to print out the keyset value. Although the KeySet object holds the reference to the containing map data, how can this exactly output the key data but not other data of the map (not value data or anything else). What is the thing that tells this KeySet object to hold only MapKeys? I can't see such instruction in its code.</p>
  
  <pre><code>package com.tutorialspoint;
  import java.util.*;
  
  public class HashMapDemo {
     public static void main(String args[]) {
  
       // create hash map
       HashMap newmap = new HashMap();
  
       // populate hash map
       newmap.put(1, "tutorials");
       newmap.put(2, "point");
       newmap.put(3, "is best");
  
       // get keyset value from map
       Set keyset = newmap.keySet();
  
       // check key set values
       System.out.println("Key set values are: " + keyset);
     }    
  }
  </code></pre>
  
  <p>Ouput : </p>
  
  <blockquote>
    <p>Key set values are: [1, 2, 3]</p>
  </blockquote>