Collections in Java

First the previous picture: about the collection interface

 

 

1. Common functions in Collection:

boolean add(Object e): add an element to the collection
void clear(): clear all elements in the collection
boolean contains(Object o): determine whether the collection contains an element
boolean isEmpty(): determine whether the element in the collection is empty
boolean remove(Object o): delete an element according to the content of the element
int size(): get the length of the collection
Object[] toArray(): can convert the collection into an array and store the elements in the collection in the array

Since Collection is an interface, it cannot be instantiated

So Collection c = new Collection(); this way of writing is wrong

 

2. Features of List subsystem:

A: Ordered (the order of storage and reading is consistent)
B: Integer index
C: Duplicate allowed

 

1. The unique function of List:

void add(int index, E element) : add an element to the index position
E get(int index) : get the element according to the index
E remove(int index) : delete the element according to the index
E set(int index, E element ): Set the element at the index index position to element

 

(1) LinkedList-specific functions:

LinkedList uses a linked list structure at the bottom, so adding and deleting is faster, and querying is slower than ArrayList
void addFirst(E e): add an element to the head of the linked list
void addLast(E e): add an element to the tail of the linked list
E getFirst(): get Elements at the head of the chain, do not delete elements
E getLast(): Get the elements at the end of the chain, do not delete elements
E removeFirst(): Return the elements at the head of the chain and delete the elements at the head of the chain
E removeLast(): Return the elements at the end of the chain and delete element at the end of the chain

 

3. Features of the Set interface:

Sets of the Set system:
A: The order in which the set is stored is inconsistent with the order in which the set is taken out
B: There is no index
C: The elements stored in the set are not repeated

 

1. HashSet uniqueness principle:

Rules: The elements newly added to the HashSet collection will be compared with the existing elements in the collection one by one.
First, the hash value will be compared (each element will call hashCode() to generate a hash value).
If the newly added element is compared with the existing elements in the collection The hash values ​​of the elements are different, and the newly added element is stored in the collection.
If the newly added element has the same hash value as an existing element in the collection, you also need to call equals(Object obj) to compare
if equals( Object obj) obj) method returns true, indicating that the newly added element has the same attribute value as an existing element in the collection, then the newly added element is not stored in the collection.
If the equals(Object obj) method returns false, it indicates that the newly added element is the same as The attribute values ​​of the existing elements in the collection are different, then the newly added element is stored in the collection

 

4. Map collection

1. Overview of the Map interface

For the collection in A:Collection, the elements exist in isolation (understood as a single), and the elements in the collection are stored one by one
in the collection in B:Map, and the elements exist in pairs (understood as husband and wife). Each element consists of a key and a value, and the key can be used to find the corresponding value.
The collection in C:Collection is called a single-column collection, and the collection in Map is called a two-column collection.
It should be noted that the collection in the Map cannot contain duplicate keys, and the values ​​can be duplicated; each key can only correspond to one value.

2.Map common functions

A: Mapping function:

V put(K key, V value) : store in the Map collection in the form of key=value

B: Get function:

V get(Object key): Get the value according to the key
int size(): Return the number of key-value pairs in the Map

C: Judgment function:

boolean containsKey(Object key): Determines whether the Map collection contains a key-value pair whose key is key
boolean containsValue(Object value): Determines whether the Map collection contains a value key-value pair
boolean isEmpty(): Determines whether there is no value in the Map collection any key-value pair

D: delete function:

void clear(): Clear all key-value pairs in the Map collection
V remove(Object key): Delete the key-value pairs in the Map according to the key value

E: Traversal function:

Set<Map.Entry<K, V>> entrySet(): Encapsulate each key-value pair into an Entry object, and then encapsulate all Entry objects into a Set collection to return
Set<K> keySet(): will All the keys in the Map are loaded into the Set collection to return
Collection<V> values(): returns the collection of all the values ​​in the collection

3. Two traversal methods of Map

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325250302&siteId=291194637