List collection
This is the back-end small class of the monastery. Each article is shared from
[background introduction] [knowledge analysis] [common problems] [solutions] [coding practice] [extended thinking] [more discussion] [References]
Eight aspects of in-depth analysis of back-end knowledge / skills. This article shares:
[list set]
Hello, I am the 12th student of Shenzhen Branch of it Academy. I am an honest, pure and kind java programmer.
Today, I'd like to share with you a knowledge point of Java task 10 on the official website of the Academy: list collection
1. Background introduction
2. Knowledge analysis
3. Frequently asked questions
4. Solutions
5. Coding practice
6. Expand thinking
7. References
8. More discussion
1. Background introduction
A collection, or container, is an object that contains multiple elements; Set can store, retrieve and operate data; The function of collection is to organize and store data in a certain way. The collection class is stored in Java Util package. Some of the collections we use are implemented in Java util. Collection this collection interface. There are three main types of sets: set, list and map.
2. Knowledge analysis
2.1. Four points to focus on in the collection
2.2. ArrayList source code analysis
2.3. Advantages and disadvantages of ArrayList
2.4. The difference between ArrayList and vector
2.1. Four points to focus on in the collection
1. Allow null
2. Allow duplicate data
3. Orderly means whether the order of reading data is consistent with the order of storing data
4. Thread safe
< Table > < tr > < td > concerns < / td > < td > conclusion < / td > < / TR > < tr > < td > whether the ArrayList is allowed to be empty < / td > < td > allow < / td > < / TR > < td > whether the ArrayList allows duplicate data < / td > < td > allow < / td > < / TR > < td > whether the ArrayList is orderly < / td > < td > orderly < / td > < / TR > < tr > < td > whether the ArrayList is thread safe < / td > < td >
Non thread safe
2.2. ArrayList source code analysis
2.3. Advantages and disadvantages of ArrayList
1. The bottom layer of ArrayList is implemented in array, which is a random access mode. In addition, it implements the randomaccess interface, so it is very fast to find, that is, get
2. ArrayList is very convenient to add an element in sequence, just adding an element to the array
However, the disadvantages of ArrayList are also obvious:
1. Deleting elements involves one element copy. If there are many elements to be copied, it will cost more performance
2. When inserting elements, it involves one element copy. If there are many elements to be copied, it will cost more performance
Therefore, ArrayList is more suitable for scenes with sequential addition and random access.
3. Frequently asked questions
3.1. ArrayList is thread unsafe, so what should be used to ensure thread safety?
3.2. ArrayList deletion is slow. Why?
3.3. Must the capacity of ArrayList meet 1.5x capacity expansion?
4. Solutions
4.1. ArrayList is thread unsafe, so what should be used to ensure thread safety?
ArrayList is thread unsafe, which is obvious because all methods in ArrayList are not synchronized, and thread safety problems will occur under concurrency. So what if we want to use ArrayList and make it thread safe? One way is to use
1234567//Collections.synchronizedList方法把你的ArrayList变成一个线程安全的L ist,比如:ListsynchronizedList=Collections.synchronizedList(list);synchronizedList.add("aaa");synchronizedList.add("bbb");for(inti=0;i stem.out.println(synchronizedList.get(i));}
Another method is vector, which is the thread safe version of ArrayList. 90% of its implementation is exactly the same as ArrayList. The difference is:
1. Vector is thread safe and ArrayList is thread unsafe
2. Vector can specify the growth factor. If the growth factor is specified, the new array size will be added to the original array size each time during capacity expansion; If the growth factor is not specified, the original array size * 2 is given. The source code is as follows:
12intnewCapacity=oldCapacity+((capacityIncrement>0)?capacityIncrement:oldCapacity);
4.2. ArrayList deletion is slow. Why?
When deleting an ArrayList, all elements after the element need to be moved forward, that is, in addition to deleting this element, many additional operations need to be done, so it takes a long time
4.3. Must the capacity of ArrayList meet 1.5x capacity expansion?
Yes, because the relevant mechanisms have been written in the source code.
1//以下为源码privatevoidgrow(intminCapacity){//overflow -conscIoUscodeintoldCapacity=elementData.length; //分析这一步,就可以得出新容量值是旧容量值的1 .5倍intnewCapacity=oldCapacity+(oldCapacity>>1);if (newCapacity-minCapacity<0)newCapacity=minCapacity;if (newCapacity-MAX_ARRAY_SIZE>0)newCapacity=hugeCapacity (minCapacity);//minCapacityisusuallyclosetosize ,sothisisawin:elementData=Arrays.copyOf(elementData ,newCapacity);}
5. Coding practice
6. Expand thinking
7. References
CSDN, Baidu Encyclopedia
8. More discussion
8.1. Under what circumstances would you use ArrayList? When will you choose LinkedList?
This is another question that most interviewers will be confused. In most cases, you should use ArrayList when you access elements more frequently than insert or delete elements. On the other hand, when you insert or delete elements more frequently in a special index, or you don't need to access elements at all, you will choose LinkedList. The main reason here is that the worst time complexity of accessing elements in ArrayList is "1", and it may be "n" in LinkedList. Adding or deleting an element in ArrayList usually calls system Arraycopy method, which is a very resource consuming operation. Therefore, the performance of LinkedList will be better when elements are frequently inserted or deleted.
8.2. How to copy an ArrayList to another ArrayList? Write your code?
Here are several techniques for copying an ArrayList to another ArrayList:
Note that 1 and 2 are shallow copies.
8.3. Differences between collections and collections
java. util. Collection is a collection interface. It provides general interface methods for basic operations on collection objects. The collection interface has many specific implementations in the Java class library. The meaning of the collection interface is to provide a maximized unified operation mode for various specific collections.
java. util. Collections is a wrapper class. It contains a variety of static polymorphic methods for collection operations. This class cannot be instantiated. It is like a tool class that serves the collection framework of Java.
That's all for today's sharing. You are welcome to like, forward, leave messages and make bricks~
Skill tree It Academy
"We believe that everyone can become an engineer. From now on, find a senior brother to introduce you, control your learning rhythm, and stop being confused on the way to learning.".
Here is the skill tree In it academy, thousands of senior brothers have found their own learning route here. Learning is transparent and growth is visible. Senior brothers have 1-to-1 free guidance.
Come and study with me~
Tencent Video:
Ppt link video link