zoukankan      html  css  js  c++  java
  • Lists, Maps and Sets in Java

    ArrayList vs LinkedList vs Vector

    From the hierarchy diagram, they all implement List interface. They are very similar to use. Their main difference is their implementation which causes different performance for different operations.  

    • ArrayList is implemented as a resizable array. As more elements are added to ArrayList, its size is increased dynamically. It's elements can be accessed directly by using the get and set methods, since ArrayList is essentially an array.

    • LinkedList is implemented as a double linked list. Its performance on add and remove is better than Arraylist, but worse on get and set methods.

    • Vector is similar with ArrayList, but it is synchronized. ArrayList is a better choice if your program is thread-safe. Because of this, it has an overhead than ArrayList. Normally, most Java programmers use ArrayList instead of Vector because they can synchronize explicitly by themselves.

    • Vector and ArrayList require space as more elements are added. Vector each time doubles its array size, while ArrayList grow 50% of its size each time.

    • LinkedList, however, also implements Queue interface which adds more methods than ArrayList and Vector, such as offer(), peek(), poll(), etc.    

    • Note: The default initial capacity of an ArrayList is pretty small. It is a good habit to construct the ArrayList with a higher initial capacity. This can avoid the resizing cost.

     

    In brief, LinkedList should be preferred if:

    • there are no large number of random access of element

    • there are a large number of add/remove operations

    HashMap vs TreeMap vs LinkedHashMap

    All three classes implement the Map interface and offer mostly the same functionality. The most important difference is the order in which iteration through the entries will happen:

    • HashMap makes absolutely no guarantees about the iteration order. It can (and will) even change completely when new elements are added.

    • TreeMap is a tree based mapping. Its put/get operations take O(log n) time. It requires items to have some comparison mechanism, either with Comparable or Comparator. TreeMap will iterate according to the "natural ordering" of the keys according to their compareTo() method (or an externally supplied Comparator). It also implements the SortedMap interfacer.  It doesn’t use equals() and hashCode() methods for comparison of elements.

    • LinkedHashMap is very similar to HashMap, but it adds awareness to the order at which items are added (or accessed), so the iteration order is the same as insertion order (or access order, depending on construction parameters)

     

    HashSet Vs TreeSet Vs LinkedHashSet In Java

      • HashSet is Implemented using a hash table. Elements are not ordered. The add, remove, and contains methods have constant time complexity O(1).

      • TreeSet is implemented using a tree structure(red-black tree in algorithm book). The elements in a set are sorted, but the add, remove, and contains methods has time complexity of O(log (n)). It offers several methods to deal with the ordered set like first(), last(), headSet(), tailSet(), etc. It doesn’t use equals() and hashCode() methods for comparision of elements.

      • LinkedHashSet is between HashSet and TreeSet. It is implemented as a hash table with a linked list running through it, so it provides the order of insertion. The time complexity of basic methods is O(1).

  • 相关阅读:
    该伙伴事务管理器已经禁止了它对远程/网络事务的支持
    HDU 4883 TIANKENG’s restaurant (贪心)
    Android:创建可穿戴应用
    debian支持ll命令
    mongodb进阶一之高级查询
    Hadoop之——又一次格式化hdfs系统的方法
    J2EE的13个规范之(二) JDBC 及其使用
    2015欧冠决赛--脑力劳动结硕果
    运行计划之误区,为什么COST非常小,SQL却跑得非常慢?
    QVariant与自定义数据类型转换的方法
  • 原文地址:https://www.cnblogs.com/codingforum/p/6561434.html
Copyright © 2011-2022 走看看