Commonly used data structure classes in Java

Structural Architecture Diagram

List

What is the difference between ArrayList, LinkedList and Vector?
  • ArrayList
    • Only reference objects can be loaded (basic types must be converted to encapsulated classes);
    • thread unsafe;
    • The bottom layer is implemented by an array (sequence table), because it is implemented by a sequence table, so it will have the characteristics of a sequence table, such as: the length needs to be declared, the expansion needs to be performed when the length is exceeded, it is not suitable for frequent movement and deletion of elements, and the retrieval of elements is fast;
    • The default capacity is 10. When it exceeds, the capacity will automatically increase by 0.5 times (oldCapacity >> 1);
  • Vector:
    • Only reference objects can be loaded (basic types must be converted to encapsulated classes);
    • Vector guarantees thread safety through the synchronized method;
    • The bottom layer is also implemented by an array;
    • The default capacity is 10 (in the construction method), and the amount of capacityIncrement is increased when it exceeds. When capacityIncrement is less than or equal to 0, it is increased by 1 times.((capacityIncrement > 0) ? capacityIncrement : oldCapacity)
  • LinkedList
    • Only reference objects can be loaded (basic types are converted to encapsulated classes);
    • thread unsafe;
    • The underlying implementation is a linked list, which has the characteristics of a linked list, such as: no need to declare the length, poor retrieval performance, but faster insertion, movement and deletion.
    • Linked list is implemented by Node object
Expansion consumption of ArrayList

Since ArrayList uses
elementData = Arrays.copyOf(elementData, newCapacity);
to expand, and
each time will recreate an array of newLength length, the space complexity of expansion is O(n), and the time complexity is O(n)

Can the List after the Arrays.asList method be expanded?

No, the List returned by asList is read-only. The reason is: the ArrayList returned by the asList method is an inner class of Arrays, and does not implement operations such as add, remove, etc.

In AbstractList, the add operation

How to make List thread safe

Collections.synchronizedList(list);
Or use manual methods to protect thread safety.

Is the List ordered?

Ordered here refers to orderliness, ordered or unordered refers to whether objects are stored in the order in which they are added, and List is ordered.

How does List implement sorting?

To implement sorting, you can use custom sorting
list.sort(new Comparator(){...})
or use Collections for quick sorting
Collections.sort(list)

How to convert between List and Array?
  • Array转List:List list = new ArrayList(array);
  • List转Array:Object [] objects = list.toArray();

Set

What is the difference between Set and List?
  • Set
    • Only reference objects can be loaded (basic types must be converted to encapsulated classes);
    • thread unsafe
    • Compared with List, it is unordered (the order of addition cannot be guaranteed), and the elements cannot be repeated
    • The bottom layer uses map for implementation (HashMap&LinkedHashMap), and uses the feature that the key of map cannot be repeated to achieve non-repetition.

The difference between HashSet, LinkedHashSet, TreeSet?

Neither can guarantee thread safety, the bottom layer uses map to achieve non-repetitiveness (so the characteristics are also in map), and Set cannot use the get(index) method to obtain elements, but can only use iterator to obtain them. in:

  • HashSet
    • With HashMap, ordering is not guaranteed.
  • LinkedHashSet
    • Using LinkedHashMap, ordering can be guaranteed
  • TreeSet
    • With NavigableMap, you can use Comparator to control the order
How to make Set thread safe

Collections.synchronizedSet(set);

Map

HashMap、LinkedHashMap、Hashtable、TreeMap的区别?
  • HashMap
    • thread unsafe
    • allow a key to be null
    • Use the hash algorithm to determine whether the key exists
    • Ordering is not guaranteed
    • The default size is 16, and each expansion will increase by 1 times by default. By default, the size of the array is 16, then when the number of elements in the HashMap exceeds 16*0.75=12 (this value is the threshold value in the code, also called the critical value), the size of the array is expanded to 2*16 =32, that is, doubled.
  • LinkedHashMap
    A subclass of HashMap that changes the structure and operations to linked list form
    • thread unsafe
    • accessOrder defaults to fasle, which guarantees orderliness
    • Based on the linear singly linked list of HashMap, a doubly linked list is internally maintained
  • Hashtable
    • The parent class is Dictionary, which is different from HashMap
    • Thread safety, methods are synchronized through synchronized
    • key&value cannot be null
    • Ordering is not guaranteed
    • The default size is 11, and the rest are consistent with HashMap
  • TreeMap
    • thread unsafe
    • Red- black tree is used internally , and the key needs to implement the Comparable interface
    • Comparator control sequence can be defined
    • Use inorder traversal when iterative traversal
How to make Map thread safe

Collections.synchronizedMap(map);

What kind of objects are suitable as keys and what are the requirements?

Classes such as String and Interger are final, immutable, and override the equals() and hashCode() methods. In other words, as a key object, immutability is necessary, because to calculate the hashCode(), it is necessary to prevent the inconsistency of the hashcode when it is put in and when it is taken out. In addition, the equals() and hashCode() methods need to be rewritten to compare object consistency.

Is the value of the capacity parameter passed in HashMap initialization the space actually allocated by HashMap?

No, it is the smallest n-th power of 2 that is larger than the incoming capacity parameter value. For example, if 6 is passed in, 8 is actually allocated.

What is the underlying data structure of HashMap?

It is an array that combines the form of a sequence list + a singly linked list, and each node inside is a Node object

appendix

Recommend several articles that describe the principle of map

Guess you like

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