title: How to implement linked list using HashMap in java
body: <p>How can I create linked list using HashMap in java? I searched online, there are implementations using LinkedList data structure. Interviewer asked me to implement it without using LinkedList data structure, I tried to do with HashTable but at the end he said I should have done it using HashMap.</p>
  
  <p>Thank you so much for answers.</p>
  
  <p>I have done this after reading your comments:</p>
  
  <pre><code>public class hashMap {
      static Map&lt;Integer, Integer&gt; mMap = new HashMap&lt;Integer, Integer&gt;();
  
          public static void main(String[] args) {
          int a;
  
          mMap.put(1, 2);
          mMap.put(2, 3);
          mMap.put(3, 4);
          mMap.put(4, 7);
          mMap.put(7, 8);
  
          Scanner in = new Scanner(System.in);
          System.out.println("Enter: ");
          a = in.nextInt();
  
              itera();
  
                      if(mMap.containsKey(a)) {
                  add.insert(a);
  
              }
                  else if (!mMap.containsKey(a)) {
                  add.remove(a);
  
              }
  
          itera();
      }
  
      public static void itera() {
          for (Iterator&lt;Integer&gt; iter = (Iterator&lt;Integer&gt;) mMap.keySet().iterator(); iter.hasNext();) {
  
              int key = iter.next();
              int sa = mMap.get(key);
              System.out.println(key + " : " + sa);
          }
  
      }
      static class add {
      public static void insert(int a) {
          int s = a-1;
          int newKey = s; 
          int sa = mMap.get(a);
          mMap.put(newKey, sa);
          mMap.remove(a);
      }
  
      public static void remove(int a) {
          int newa = a;
          while(!mMap.containsKey(a)) {
              a--;
              System.out.println("while a: " + a);
  
          }
  
          mMap.put(newa, mMap.get(a));
  
          mMap.put(a, newa);
  
      }
  }
  
  
  }
  </code></pre>
  
  <p>It just inserts and deletes nodes into Linked List. But there is problem if some keys are missing, like 5 &amp; 6 is not there in keys. So if I try to insert 6, it doesn't work.  Could anyone explain what am I doing wrong?</p>