当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

HashMap与TreeMap

发表于: 2014-05-11   作者:darrenzhu   来源:转载   浏览:
摘要: 区别: 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 ov

区别:
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.







HashMap与TreeMap

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号