Collection summary of Java foundation

set

A collection is a container provided by Java that can be used to store multiple data.

The collection is divided into single-column collection and double-column collection

Single column collection

The root interface of a single-column set is Collection, which is used to store a series of elements that conform to certain rules. It has two important sub-interfaces, java.util.List and java.util.Set.

There are two main implementation classes under the List interface, ArrayList and LinkedList.

There are two main implementation classes under the Set interface, HashSet and LinkedHashSet.

Traverse method

Iterators and enhanced for

ArrayList

Use array to save data

It is characterized by fast query and slow addition and deletion.

Why is the query fast?

Arrays are stored continuously in memory. Continuous storage means that the elements in the array are adjacent in memory. In this way, the address values ​​of other elements can be calculated based on the address value of the first element of the array.

Why is the addition and deletion slow?

Because the length of the array is fixed, a new array needs to be created for each addition or deletion operation.

The default capacity is 10.

The expansion increment is: 0.5 times the original capacity + 1

If the initial capacity is 10, the capacity after one expansion is 16.

LinkedList

Use doubly linked list to save data

It is characterized by slow query and fast addition and deletion.

Why is the query slow?

Linked lists are stored discretely in memory

Discrete storage means that the nodes of the linked list are irregular in memory. So if you find a node, you must find one by one from the previous.

Why add and delete quickly?

If you want to add or delete operations, you only need to modify the address value of the linked list node.

HashSet

The bottom layer is to use HashMap to store data

There are three characteristics

1. Disorder

2. No index

Three. Not repeatable

The initial capacity is 16.

The load factor is 0.75: when the number of elements exceeds 0.75 times the capacity length, expand the capacity

Expansion increment: 1 times the original capacity.

​ For example, the capacity of HashSet is 16, and after one expansion, it is 32.

The way HashSet guarantees uniqueness

There are two steps

First, compare the hash values ​​of two objects

​ If the hash value is different, it must be a different object

​ If the hash values ​​are the same, they are not necessarily the same object

Second, use the equals method to compare

​ If the result of the equals method is true, it means that the two objects are the same

​ If the result of the equals method is false, it means that the two objects are different.

If you want to ensure the uniqueness of the key in the HashMap, you must rewrite the corresponding hashCode and equals.

LinkedHashSet

The internal use of hash table + linked list to store data can ensure order.

The Set interface is unordered, but it cannot guarantee that all implementation classes are unordered.

Double row set

The root interface of a two-column set is Map, and there are three main implementation classes HashMap, Hashtable, and TreeMap below.

Each element in it is a key-value pair. The key cannot be repeated, and the value is allowed to be repeated.

We can find the value of value based on the value of the key.

Traverse method

keySet

Get all the keys in the Map collection, put all the keys into a Set collection, traverse the Set collection
to get each key in it, and get the corresponding value according to the key.

entrySet

Get all Entry objects in the Map collection and put them in the Set collection. Then traverse.

In Entry, the methods to get the key and value:
K getKey(): Get the key in the Entry object
V getValue(): Get the value in the Entry object.

HashMap

The initial capacity is 16.

The load factor is 0.75: when the number of elements exceeds 0.75 times the capacity length, expand the capacity

Expansion increment: 1 times the original capacity.

​ For example, the capacity of HashSet is 16, and after one expansion, it is 32.

How HashMap guarantees uniqueness

There are two steps

First, compare the hash values ​​of two objects

​ If the hash value is different, it must be a different object

​ If the hash values ​​are the same, they are not necessarily the same object

Second, use the equals method to compare

​ If the result of the equals method is true, it means that the two objects are the same

​ If the result of the equals method is false, it means that the two objects are different.

If you want to ensure the uniqueness of the key in the HashMap, you must rewrite the corresponding hashCode and equals.

Features of LinkedHashMap

The bottom layer is hash table + linked list, which can guarantee order.

The difference between HashMap and HashTable

  1. HashMap keys and values ​​are allowed to be empty, and HashTable is not allowed to be empty.
  2. HashMap is not thread safe, HashTable is based on Synchronized, thread safe
  3. HashMap is asynchronous, suitable for single thread, HashTable is synchronized, suitable for multi-thread.
  4. HashMap is efficient and fast, while HashTable is inefficient.

The difference between ConcurrentHashMap and HashTable

  1. ConcurrentHashTable uses lock segmentation technology to ensure thread safety, HashTable uses Synchronized to ensure thread safety
  2. The lock mechanism used by ConcurrentHashTable is to lock one bucket at a time. By default, ConcurrentHashTable divides the Hash table into 16 buckets, which can be executed by 16 writing threads at the same time, which greatly improves the concurrent performance. The lock mechanism used by HashTable is to lock the entire hash at once table.

Guess you like

Origin blog.csdn.net/numbbe/article/details/109321992