who is faster hashmap.get or treemap.get in java

This question already has an answer here:

  • Difference between HashMap, LinkedHashMap and TreeMap 16 answers

  • TreeMap is a binary search tree implementation of the Map interface. Therefore any lookup operation requires O(logN) time.

    On the other hand, HashMap uses the hashCode() of the key to locate the bin that contains the key in constant time. Since each bin has an expected number of entries bound by a small constant, lookup requires O(1) time, which is faster than O(logN) .


    As simple as, HashMap put / get method uses the hashCode() and equals() method, whereas TreeMap use the some comparison mechanism using Comparable or Comparator .

    One more point,

    HashMap is more time-efficient. A TreeMap is more space-efficient.

    链接地址: http://www.djcxy.com/p/92180.html

    上一篇: 我怎样才能比较两个输入值到两个数组

    下一篇: 谁在Java中更快的hashmap.get或treemap.get