who is faster hashmap.get or treemap.get in java
This question already has an answer here:
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.
上一篇: 我怎样才能比较两个输入值到两个数组
