Originally I was going to have a chat on the experience on a straight talk about the collection, the result has written a lot of basic can not stop, so decided to open a new article
Since the collection is to be studied, the resources on the network are generally analyzed from 4 dimensions.
1. Whether to allow null
2. Whether duplicate data is allowed
3. Whether ordered: the order of storage and the order of reading is consistent
4. Whether the thread is secure.
First, let's take a look at ArrayList.
| Whether to allow empty |
Can be empty |
| Whether to allow duplicates |
Can repeat |
| is ordered |
Ordered |
| is thread safe |
Thread not secure |
First put a piece of code to see, verify
New Arraylist<string>(); List.add ("a"); List.add (null); List.add ("a"); List.add ("B"); System.out.println (List.tostring ());
[A, NULL, a, b]
The Add method is fast and has no synchronized adornments, so threads are unsafe
Public Boolean Add (e e) { + 1); // increments modcount!! elementdata[size++] = e; return true ;}
The collection framework for Java