Chapter 7 Summary of common Java Collection classes and Chapter 7 java
7.1. List (repeated elements are allowed)
- ArrayList:
- Underlying data structure: Object []
- InGet, iterator, and set)ArrayList
- Scalable, unlimited capacity
- Shortlist
- Underlying data structure: Annular two-way linked list
- InAdd and remove)In the case of many operations, use the upload list
- Linked List, unlimited capacity
Note:
1) add (E): inserts an element at the end of the array. ArrayList needs to consider the expansion problem. Once the expansion is completed, array copying is required, and the sort list is not required;
2) add (int index): insert an element in the middle of the array. In ArrayList, you must consider copying all the index and its array elements and moving them one by one.
7.2. Set (repeated elements are not allowed, so they can be used for deduplication)
- HashSet:
- Underlying data structure: HashMap
- Can be seen as unlimited capacity
- TreeSet:
- Underlying data structure: TreeMap
- Unlimited capacity
7.3 Map (key-value)
- HashMap:
- Underlying data structure: linked list Array
- Scalable, and the maximum capacity is large, can be seen as unlimited capacity
- TreeMap:
- Underlying data structure: red/black tree
- Sort by key(In use, either use TreeMap (Comparator) or let the key object implement Comparable)
- Red/black tree with unlimited capacity
Note:
- All the above threads are not secure
- When the search and deletion are frequent and the number of elements is large (the number of elements is greater than 100), the Set and Map performance is better than the List (in the case of single thread)
The second conclusion above is in the book distributed Java basics: Applications and practices, Lin Hao obtained through a series of test results.