Java Collection Collection Summary-Power Node

Collection

Can only store reference type data, single storage

Basic operations: add(), remove(), contains(), size(), iterator()

List collection

Features: The stored elements are orderly and repeatable

Specify an index value for each element

Increased method, operation for index value, listIterator(), subList(), sort(Comparator)

ArrayList collection

The bottom layer is an array, which is fast to access and inefficient to add/remove

Initialization capacity: 10, expansion: 1.5 times

Vector collection

The bottom layer is an array, it is thread-safe, ArrayList is not thread-safe

Initialization capacity: 10, expansion: 2 times

LinkedList collection

The bottom layer is a doubly linked list, which has high add/delete efficiency and slow access

List collection application scenarios

ArrayList is suitable for access-oriented, rarely add/delete situations

LinkedList is suitable for frequently added/deleted situations

Set collection

Features: Data is disordered and cannot be repeated

HashSet collection

The bottom layer is HashMap, HashSet is actually a collection of HashMap keys

TreeSet collection

The bottom layer is TreeMap, and TreeSet is actually a collection of TreeMap keys

TreeSet implements the SortedSet interface, which can sort elements naturally, and requires elements to be comparable:

● Specify the Comparator object in the constructor

● If there is no Comparator, the class of the collection element must implement the Comparable interface

Set collection application scenarios

If you don't need to sort the Set collection, choose HashSet

If you need to sort the elements of the Set collection, choose TreeSet

note:

Methods such as the contains( e) / remove( e) of the List collection/HashSet collection need to call the equals() method of the object, and the classes of the elements in these collections need to override the equals() method

Methods such as contains( e )/remove( e) in the TreeSet collection determine whether the same object is the same object according to whether the comparison result of Comparator/Comparable is 0. If the comparison result is 0, it means the same element

Guess you like

Origin blog.csdn.net/weixin_49543720/article/details/110237637