Rule of thumb for choosing an implementation of a Java Collection?

前端 未结 11 1471
执念已碎
执念已碎 2020-11-29 16:06

Anyone have a good rule of thumb for choosing between different implementations of Java Collection interfaces like List, Map, or Set?

For example, generally why or i

相关标签:
11条回答
  • 2020-11-29 16:56

    About your first question...

    List, Map and Set serve different purposes. I suggest reading about the Java Collections Framework at http://java.sun.com/docs/books/tutorial/collections/interfaces/index.html.

    To be a bit more concrete:

    • use List if you need an array-like data structure and you need to iterate over the elements
    • use Map if you need something like a dictionary
    • use a Set if you only need to decide if something belongs to the set or not.

    About your second question...

    The main difference between Vector and ArrayList is that the former is synchronized, the latter is not synchronized. You can read more about synchronization in Java Concurrency in Practice.

    The difference between Hashtable (note that the T is not a capital letter) and HashMap is similiar, the former is synchronized, the latter is not synchronized.

    I would say that there are no rule of thumb for preferring one implementation or another, it really depends on your needs.

    0 讨论(0)
  • 2020-11-29 16:57

    Well, it depends on what you need. The general guidelines are:

    List is a collection where data is kept in order of insertion and each element got index.

    Set is a bag of elements without duplication (if you reinsert the same element, it won't be added). Data doesn't have the notion of order.

    Map You access and write your data elements by their key, which could be any possible object.

    Attribution: https://stackoverflow.com/a/21974362/2811258

    For more information about Java Collections, check out this article.

    0 讨论(0)
  • 2020-11-29 17:04

    I found Bruce Eckel's Thinking in Java to be very helpful. He compares the different collections very well. I used to keep a diagram he published showing the inheritance heirachy on my cube wall as a quick reference. One thing I suggest you do is keep in mind thread safety. Performance usually means not thread safe.

    0 讨论(0)
  • 2020-11-29 17:04

    As suggested in other answers, there are different scenarios to use correct collection depending on use case. I am listing few points,

    ArrayList:

    • Most cases where you just need to store or iterate through a "bunch of things" and later iterate through them. Iterating is faster as its index based.
    • Whenever you create an ArrayList, a fixed amount of memory is allocated to it and once exceeeded,it copies the whole array

    LinkedList:

    • It uses doubly linked list so insertion and deletion operation will be fast as it will only add or remove a node.
    • Retrieving is slow as it will have to iterate through the nodes.

    HashSet:

    • Making other yes-no decisions about an item, e.g. "is the item a word of English", "is the item in the database?" , "is the item in this category?" etc.

    • Remembering "which items you've already processed", e.g. when doing a web crawl;

    HashMap:

    • Used in cases where you need to say "for a given X, what is the Y"? It is often useful for implementing in-memory caches or indexes i.e key value pairs For example: For a given user ID, what is their cached name/User object?.
    • Always go with HashMap to perform a lookup.

    Vector and Hashtable are synchronized and therefore bit slower and If synchronization is needed, use Collections.synchronizedCollection(). Check This for sorted collections. Hope this hepled.

    0 讨论(0)
  • 2020-11-29 17:07

    For non-sorted the best choice, more than nine times out of ten, will be: ArrayList, HashMap, HashSet.

    Vector and Hashtable are synchronised and therefore might be a bit slower. It's rare that you would want synchronised implementations, and when you do their interfaces are not sufficiently rich for thier synchronisation to be useful. In the case of Map, ConcurrentMap adds extra operations to make the interface useful. ConcurrentHashMap is a good implementation of ConcurrentMap.

    LinkedList is almost never a good idea. Even if you are doing a lot of insertions and removal, if you are using an index to indicate position then that requires iterating through the list to find the correct node. ArrayList is almost always faster.

    For Map and Set, the hash variants will be faster than tree/sorted. Hash algortihms tend to have O(1) performance, whereas trees will be O(log n).

    0 讨论(0)
提交回复
热议问题