title: Sorting HashMap<?,?> by key
body: <p>hello
  I need to implement a method that receives a HashMap and sorts (mergeSort) it's values by key <strong>(without using TreeMap, SortedMap or Collections.Sort or use any sort solutions from JAVA Packages)</strong>.
  my problem is dealing with the wildcard Types...
  this is my implementation (that returns compilation errors because of wildcards use)</p>
  
  <pre><code>public HashMap&lt;?, ?&gt; mergeSort(HashMap&lt;?, ?&gt; map) {
          if (map.size() &lt; 1) {
              return map;
          }
          // rounds downwards
          int middle = map.size() / 2;
          int location = 0;
  
          HashMap&lt;?,?&gt; mapLeft = new HashMap&lt;?, ?&gt;();
          HashMap&lt;?,?&gt; mapRight = new HashMap&lt;?, ?&gt;();
          // splitting map
          for (Iterator&lt;?&gt; keyIter = map.keySet().iterator(); keyIter.hasNext();) {
              if (location &lt; middle) {
                  mapLeft.put(keyIter, map.get(keyIter));
              } else {
                  mapRight.put(keyIter, map.get(keyIter));
              }
              location++;
          }
          // recursive call
          mapLeft = mergeSort(mapLeft);
          mapRight = mergeSort(mapRight);
          return merge(mapLeft, mapRight);
      }
  
      public HashMap&lt;?, ?&gt; merge(HashMap&lt;?, ?&gt; mapLeft, HashMap&lt;?, ?&gt; mapRight) {
          HashMap&lt;?, ?&gt; result = new HashMap&lt;?, ?&gt;();
          Iterator&lt;?&gt; keyLeftIter = mapLeft.keySet().iterator();
          Iterator&lt;?&gt; keyRightIter = mapRight.keySet().iterator();
          String keyLeft;
          String keyRight;
          while (keyLeftIter.hasNext()) {
              keyLeft = keyLeftIter.next();
              while (keyRightIter.hasNext()) {
                  keyRight = keyRightIter.next();
  
                  if (keyLeft.compareTo(keyRight) &lt; 0) {
                      result.put(keyLeft, mapLeft.get(keyLeft));
                      keyLeft = keyLeftIter.next();
                  } else {
                      result.put(keyRight, mapRight.get(keyRight));
                      keyRight = keyRightIter.next();
                  }
              }
          }
          return result;
      }
  </code></pre>
  
  <p>I appreciate your help!</p>