`
darrenzhu
  • 浏览: 784077 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

HashMap与TreeMap

    博客分类:
  • Java
阅读更多

区别:
1) HashMap是不保证里面元素顺序的,尤其是,随着时间的推移,它也不能保证顺序的不变。
   This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.
  
  TreeMap是实现了SortedMap接口,是有序的,TreeMap是一颗红黑树(自平衡的二叉排序树,且节点带有颜色)

2) HashMap的基本操作get,put的时间复杂度是常数O(1),

   This implementation provides constant-time performance for the basic operations (get and put), assuming the hash function disperses the elements properly among the buckets. Iteration over collection views requires time proportional to the "capacity" of the HashMap instance (the number of buckets) plus its size (the number of key-value mappings). Thus, it's very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important.

   而TreeMap的containsKey, get, put, remove操作的时间复杂度是log(n)
   This implementation provides guaranteed log(n) time cost for the containsKey, get, put and remove operations. Algorithms are adaptations of those in Cormen, Leiserson, and Rivest's Introduction to Algorithms.







分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics