Java list of implementation classes

Java list of implementation classes

This article is based on Bowen finishing

In Java, List interfaces to achieve a total of three classes: ArrayList, Vector and LinkedList.

Wherein ArrayList and Vector are the use of an array data structure to achieve this, it has a strong random access capability, but the corresponding ability to insert and delete the relatively weak: ArrayList and Vector required between elements of the array is used to achieve No gaps are , in other words, if you delete an element in the middle, must move forward to fill the seats behind. I guess this is to prevent fragmentation of the array of problems, but at the end of the insertion and deletion of the cost is relatively small. Another problem is the array initialization time to agree on a size, so when the number of elements when the insert is larger than the maximum size of the current space, you need to apply for a new memory space, creates a new array and copy the existing data in the past (ArrayList default when not enough memory is an extension of 50% + 1, Vector is the default extension 1 times), it is also very time-consuming. So both for random access and traverse, it is not suitable for a large number of additions and deletions.

The difference between Vector and ArrayList is that the synchronization between the Vector support threads, that is, only one thread can write to the Vector at the same time. But to do this function takes a costly, slow to use.

LinkedList is implemented using the list, then the corresponding, its cost of additions and deletions is relatively small, but the ability to randomly access on the weak. ListedList also defines a method of more than List interface requirements, such as elemental operations tail header and can be used as queues, stacks or bidirectional list used.

Exactly what kind of implementation class to use depends on the specific usage scenarios. If you do not relate to multi-threaded, then you do not have nothing Vector, ArrayList when more random visits, LinkedList for more additions and deletions situation .

Guess you like

Origin www.cnblogs.com/jiading/p/11914122.html