Java Novice Learning Guide [day17]---Set-Set+Set-List

1. Collection (conllection (interface))

Iterators can be used during traversal

Iterator usage:

1. Obtain the iterator of the corresponding collection according to the objects of the collection

2. Call hasNext() to determine if there is a next value

3. Get the next value through the next() method

2、List

Any number of elements can be repeated, including null. In addition to iterators, there are also foreach and for loops.

1. ArrayList (based on array)

Thread is not safe and orderly (the order of addition is the same as the order of output), because the data is based on an array, so it has an index, which can be quickly located according to the index, so it is faster to find and modify

Adding and deleting are slow, because adding involves the creation of new arrays and the copying of data, which consumes more performance and memory at this time

2. LinkedList (based on linked list)

Thread is not safe and orderly (the order of addition is the same as the order of output), because it is based on a linked list, so the expansion is faster, so adding elements is faster

It is relatively slow to traverse the entire linked list when querying or deleting data

2、set

Non-repeatable, traversal methods include foreach in addition to iterators

1. HashSet (set implementation class)

Thread is not safe, unordered (adding order is inconsistent with output order), can store any number of data, including null

The bottom layer is based on HashMap, and the deduplication is mainly determined by hashCode and equals

2. TreeSet (set implementation class)

Thread is unsafe, disordered (adding order is inconsistent with output order), can store any number of data, excluding null, but can only store the same type of data at the same time

The bottom layer is based on TreeMap, and there are two ways to remove duplicates:

Natural ordering: the entity class implements the Comparable interface

Custom sorting: implement Comparator to hand over the comparator to the collection container

Guess you like

Origin blog.csdn.net/WLK0423/article/details/109631273