Java collection List, Set and Map difference and summary of applicable scenarios

  1. List and Set are both inherited from the Collection interface, Map is not

  2. List elements have an order in which they are placed, and elements can be repeated. Similar to an array, the List can grow dynamically. The efficiency of finding elements is high, and the efficiency of inserting and deleting elements is low, because it will cause the position of other elements to change. Set elements have no order, elements cannot be repeated, duplicate elements will be overwritten, Set retrieval elements are inefficient, deletion and insertion are efficient, and insertion and deletion will not cause element positions to change.

  3. Map is an interface suitable for storing data of key-value pairs

  4. Thread-safe collection classes and non-thread-safe collection classes

    • LinkedList, ArrayList are not thread safe, Vector is thread safe

    • HashMap, HashSet are not thread safe, HashTable is thread safe

    • StringBuilder is not thread safe, StringBuffer is thread safe

  5. Differences and applicable scenarios between ArrayList and LinkedList

    • Arraylist:

      Advantages: ArrayList implements a data structure based on dynamic arrays. Because the addresses are continuous, once the data is stored, the query operation efficiency will be relatively high (it is connected in memory).

      Disadvantages: Because the addresses are continuous, ArrayList needs to move data, so the efficiency of insertion and deletion operations is relatively low.

    • LinkedList:

      Advantages: LinkedList is based on the data structure of the linked list, and the address is arbitrary, so there is no need to wait for a continuous address when opening up memory space. For add and remove operations, LinedList has an advantage. LinkedList is suitable for scenarios where head and tail operations are to be performed or inserted at a specified position

      Disadvantage: Because LinkedList needs to move the pointer, the query operation performance is relatively low.

    • Applicable scenario analysis:

      Use ArrayList when you need to access data, and use LinkedList when you need to add, delete, and modify data multiple times.

  6. Differences and applicable scenarios between ArrayList and Vector

    • ArrayList has three constructors:

      public ArrayList(int initialCapacity)//构造一个具有指定初始容量的空列表。    
      public ArrayList()//构造一个初始容量为10的空列表。    
      public ArrayList(Collection<? extends E> c)//构造一个包含指定 collection 的元素的列表      
    • Vector has four constructors:

      public Vector()//使用指定的初始容量和等于零的容量增量构造一个空向量。    
      public Vector(int initialCapacity)//构造一个空向量,使其内部数据数组的大小,其标准容量增量为零。    
      public Vector(Collection<? extends E> c)//构造一个包含指定 collection 中的元素的向量    
      public Vector(int initialCapacity,int capacityIncrement)//使用指定的初始容量和容量增量构造一个空的向量    
    • Both ArrayList and Vector are implemented with arrays, and there are three main differences:

      1), Vector is multi-thread safe. Thread safety means that multiple threads access the same code without producing indeterminate results. ArrayList is not. This can be seen from the source code. Many methods in the Vector class are modified by synchronized, which makes Vector unable to compare with ArrayList in efficiency.

      2) Both of them use linear continuous space to store elements, but when the space is insufficient, the ways of adding the two classes are different.

      3), Vector can set the growth factor, but ArrayList cannot.

      4), Vector is an old dynamic array, which is thread-synchronized, and the efficiency is very low. Generally, it is not recommended to be used.

    • Applicable scenario analysis:

      1), Vector is thread-synchronized, so it is also thread-safe, while ArrayList is thread-asynchronous and unsafe. If the thread safety factor is not considered, generally using ArrayList is more efficient.

      2) If the number of elements in the collection is greater than the length of the current collection array, and the data with a relatively large amount of data is used in the collection, using Vector has certain advantages.

  7. Applicable scenarios of HashSet and Treeset

    • TreeSet is implemented by binary tree (the tree data structure of red-black tree). The data in Treeset is automatically sorted, and null values ​​are not allowed.

    • HashSet is implemented by a hash table. The data in HashSet is disordered and can be put in null, but only one null can be put in. The values ​​in both cannot be repeated, just like the unique constraint in the database

    • HashSet requires that the placed objects must implement the hashCode() method. The placed objects are identified by the hashCode code, and the String objects with the same content have the same hashCode, so the placed content cannot be repeated, but the same class objects can be put into different instances

    • Applicable scenario analysis:

      HashSet is implemented based on Hash algorithm, and its performance is usually better than TreeSet. Sets designed for fast search, we should usually use HashSet, when we need sorting function, we use TreeSet.

  8. Differences and applicable scenarios between HashMap, TreeMap and HashTable

    • HashMap is not thread-safe and is implemented based on hash tables. Using HashMap requires that the added key class has hashCode() and equals() explicitly defined [hashCode() and equals() can be overridden], in order to optimize the use of HashMap space, you can tune the initial capacity and load factor.

    • TreeMap is not thread-safe and is implemented based on red-black trees. TreeMap has no tuning options because the tree is always in equilibrium.

    • Applicable scenario analysis:

      1) Difference between HashMap and HashTable

      HashMap removes the contains method of HashTable, but adds the containsValue() and containsKey() methods.

      HashTable is synchronous, while HashMap is asynchronous, which is more efficient than HashTable.

      HashMap allows null key values, while HashTable does not.

      2) Difference between HashMap and Treemap

      HashMap is suitable for inserting, deleting and positioning elements in Map.

      Treemap: suitable for traversing keys in natural or custom order.

  9. Set

    • No duplicate objects are allowed
       
        - unordered containers, you can't guarantee the storage order of each element, TreeSet maintains a sort order through Comparator or Comparable.

    • Only one null element is allowed

    • The most popular implementations of the Set interface are HashSet, LinkedHashSet, and TreeSet. The most popular is HashSet based on HashMap; TreeSet also implements the SortedSet interface, so TreeSet is an ordered container sorted according to the definitions of its compare() and compareTo().

  10. Map

    • Map is not a sub-interface or implementation class of Collection, Map is an interface.

    • Each Entry of the Map holds two objects, that is, a key and a value. Maps may hold identical value objects, but key objects must be unique.

      • TreeMap also maintains a sort order via Comparator or Comparable.

      • You can have as many null values ​​as you want in a Map but there can only be at most one null key.

      • Some of the most popular implementation classes of the Map interface are HashMap, LinkedHashMap, Hashtable, and TreeMap. (HashMap, TreeMap are most commonly used)

  11. In what scenarios do you use list, set, map?

    • If you often use the index to access the elements in the container, then List is the right choice for you. If you already know the index, then List implementation classes such as ArrayList can provide faster access. If you often add and remove elements, you must choose LinkedList.

    • If you want the elements in the container to be stored in the order in which they were inserted, then it is still a List, because List is an ordered container and it is stored in the order of insertion.

    • If you want to ensure the uniqueness of inserted elements, that is, you do not want to have duplicate values, then you can choose a Set implementation class, such as HashSet, LinkedHashSet or TreeSet. All Set implementation classes follow unified constraints such as uniqueness, and also provide additional features: For example, TreeSet is also a SortedSet, and all elements stored in TreeSet can be sorted using Comparator or Comparable in Java. LinkedHashSet also stores elements in the order in which they were inserted.

    • If you store data in the form of keys and values ​​then Map is the right choice for you. You can choose from Hashtable, HashMap, TreeMap according to your subsequent needs.

  12. SparseArray<E>

    A binary method is used to store data (a collection class of Android)

    The android system suggests that we SparseArray<E>use insteadHashMap<Integer, E>

    The use of SparseArray is basically the same as that of List

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325768271&siteId=291194637