Java Collections效能比較

來源:互聯網
上載者:User
Java Collections效能比較 Uncategorized,
by Zen.

其實這是轉貼,原文地址。有時間再簡單翻譯下。

List – this
is an ordered list of objects, insertion order is maintained and retrieval order is in the list order but items can also be random accessed, duplicate items are allowed, generally allow storage of null values (the ones below do), generally fast to iterate
and find items by position but slow to do lookups

  • ArrayList –
    Unsychronized, nulls allowed (fastest)
  • Vector – Synchronized,
    only slightly slower in tests of sizes under 100000
  • Stack – Synchronized,
    same speed as Vector, LIFO queue
  • LinkedList –
    Unsynchronized, allows two way iteration and modification of items (like a stack or queue)
  • CopyOnWriteArrayList –
    Synchronized, significantly slower in tests of large numbers of items or average list changes, only slightly slower when used with very small numbers (<100)>


Set – this a
set of items with no duplicates (no two items can compare as equal), ordering is typically inconsistent over multiple set iterations depending on the implementation but you should assume the order is effectively random unless the set specifies ordered iteration,
generally ok to iterate and fast to do lookups

  • HashSet –
    Unsychronized (fastest), slower than HashMap which it is built on, allows nulls
  • LinkedHashSet –
    Unsychronized, ordered by insertion, allows nulls
  • TreeSet –
    Unsychronized, ordered by the natural ordering of the items or a comparator provided
    at construction, allows nulls but there are issues
    with removing them
  • CopyOnWriteArraySet –
    Synchronized, significantly slower in tests of large numbers of items or average set changes, only slightly slower when used with very small numbers (<100)>

Map – Stores
key/value pairs (maps keys to values) where the keys must be unique, order of iteration over keys, values, or pairs is highly dependent on the implementation of the map, allowed nulls also vary by implementation, generally very fast to lookup keys and slow
to lookup values

  • IdentityHashMap –
    Unsychronized (fastest), uses reference equality (==) instead of object equality (equals) to compare keys, actually violates the Map interface guarantee, all iterators are unordered, allows null keys and values
  • HashMap –
    Unsychronized, this is the fastest general purpose map, all iterators are unordered, allows null keys and values
  • ConcurrentHashMap –
    Synchronized, all iterators are unordered, does not allow null keys or values
  • Hashtable –
    Synchronized, all iterators are unordered, does not allow null keys or values
  • LinkedHashMap –
    Unsychronized, all iterators are ordered based on insertion order of the original key (does not change if a key is reinserted), allows null values but null keys are not allowed
  • TreeMap –
    Unsychronized, iterators are ordered by the natural or comparator ordering
    of the keys, allows null keys and values but the comparator needs to understand them
Uncategorized,
by Zen.

其實這是轉貼,原文地址。有時間再簡單翻譯下。

List – this
is an ordered list of objects, insertion order is maintained and retrieval order is in the list order but items can also be random accessed, duplicate items are allowed, generally allow storage of null values (the ones below do), generally fast to iterate
and find items by position but slow to do lookups

  • ArrayList –
    Unsychronized, nulls allowed (fastest)
  • Vector – Synchronized,
    only slightly slower in tests of sizes under 100000
  • Stack – Synchronized,
    same speed as Vector, LIFO queue
  • LinkedList –
    Unsynchronized, allows two way iteration and modification of items (like a stack or queue)
  • CopyOnWriteArrayList –
    Synchronized, significantly slower in tests of large numbers of items or average list changes, only slightly slower when used with very small numbers (<100)>


Set – this a
set of items with no duplicates (no two items can compare as equal), ordering is typically inconsistent over multiple set iterations depending on the implementation but you should assume the order is effectively random unless the set specifies ordered iteration,
generally ok to iterate and fast to do lookups

  • HashSet –
    Unsychronized (fastest), slower than HashMap which it is built on, allows nulls
  • LinkedHashSet –
    Unsychronized, ordered by insertion, allows nulls
  • TreeSet –
    Unsychronized, ordered by the natural ordering of the items or a comparator provided
    at construction, allows nulls but there are issues
    with removing them
  • CopyOnWriteArraySet –
    Synchronized, significantly slower in tests of large numbers of items or average set changes, only slightly slower when used with very small numbers (<100)>

Map – Stores
key/value pairs (maps keys to values) where the keys must be unique, order of iteration over keys, values, or pairs is highly dependent on the implementation of the map, allowed nulls also vary by implementation, generally very fast to lookup keys and slow
to lookup values

  • IdentityHashMap –
    Unsychronized (fastest), uses reference equality (==) instead of object equality (equals) to compare keys, actually violates the Map interface guarantee, all iterators are unordered, allows null keys and values
  • HashMap –
    Unsychronized, this is the fastest general purpose map, all iterators are unordered, allows null keys and values
  • ConcurrentHashMap –
    Synchronized, all iterators are unordered, does not allow null keys or values
  • Hashtable –
    Synchronized, all iterators are unordered, does not allow null keys or values
  • LinkedHashMap –
    Unsychronized, all iterators are ordered based on insertion order of the original key (does not change if a key is reinserted), allows null values but null keys are not allowed
  • TreeMap –
    Unsychronized, iterators are ordered by the natural or comparator ordering
    of the keys, allows null keys and values but the comparator needs to understand them
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.