title: Performance of Sorting a HashMap by Value vs Using TreeMap or LinkedHashMap
body: <p>I'm currently sorting a HashMap by value and was wondering whether performance would be better if  it was better to sort using something like a TreeMap instead.</p>
  
  <p>I know that HashMap's have an insertion and sorting complexity of o(1) time and a tree sort in java will take log(n) (I believe), but I'm wondering what the cost of sorting a HashMap actually is. </p>
  
  <p>I'm currently sorting with: </p>
  
  <pre><code> public static HashMap sortByValue(Map unsortMap) {
      List list = new LinkedList(unsortMap.entrySet());
  
      Collections.sort(list, new Comparator() {
          public int compare(Object o1, Object o2) {
              return ((Comparable) ((Map.Entry) (o2)).getValue())
                      .compareTo(((Map.Entry) (o1)).getValue());
          }
      });
  
      HashMap sortedMap = new LinkedHashMap();
      for (Iterator it = list.iterator(); it.hasNext();) {
          Map.Entry entry = (Map.Entry) it.next();
          sortedMap.put(entry.getKey(), entry.getValue());
      }
      return sortedMap;
  }
  </code></pre>
  
  <p>Which method would be best to use and why?</p>
  
  <p>Edit: I meant searching, not sorting complexity. </p>