Java foundation – Collection

Collection framework

Java. util. Collection

Common functions in the collection interface

1. Add

booblean add(Object obj); Add elements to the collection, one at a time

boolean addAll(Collection c); Adds all elements in the specified collection to this collection

2. Delete

void clear(); Remove all elements in this collection

boolean remove(Object o); Removes a single instance of the specified element, if any, from this collection

boolean removeAll(Collection e); Remove all elements in this collection that are also included in the specified collection

3. Obtain

int size(); Returns the number of elements in this collection.

4. Judgment

boolean isEmpty();

boolean contains(Object o); Returns true if the collection contains the specified element.

boolean containsAll(Collection c); Returns true if this collection contains all elements in the specified collection.

5. Convert the set into an array

toArray(); Returns an array containing all elements in this collection.

toArray(T[ ] a); Returns an array containing all elements in this collection; The runtime type of the returned array is the same as that of the specified array.

6. Take out the collection elements

Iterator iterator(); Obtain the iterator object of the iteration function on the element in the collection;

Returns the iterator that iterates over the elements of this collection.

Iteration: a way to extract elements;

Iterator: object with iteration function;

The iterator object does not need new, but can be obtained directly through the iterator () method. The iterator is a public method to extract the elements in the collection

Collection

| -- List: ordered (the order of storage is consistent with the order of extraction). There are indexes, and repeated elements are allowed

| -- Set: duplicate elements are not allowed

Focus on the unique methods in the list interface: its unique methods are defined around the index List supports addition, deletion, modification and query;

1. Increase

add(index,element);

2. Delete

remove(index);

3. Change

set(index,new element);

4. Check

int indexOf(element);

element get(index);

Specific subclasses of the list set; Subclasses are distinguished because the internal data interfaces (the way data is stored) are different.

JDK1. 0 | -- vector: the data structure is an array; The array is of variable length (continuously new new array and copy the original array elements to the new array). The thread is synchronous, and the addition, deletion and query are slow.

JDK1. 2 | -- ArrayList: it is also an array structure with variable length. The threads are not synchronized, which replaces vector. The addition and deletion speed is not fast, and the query speed is very fast.

|--LinkedList: linked list structure. Threads are not synchronized. The addition and deletion speed is fast, and the query speed is slow.

Linkedlisgt's special method:

1. Add elements

addFirst(E e); Inserts the specified element at the beginning of this list.

Addlast (E): adds the specified element to the end of this list.

2. Get elements

getFirst(); Returns the first element of this list.

getLast(); Returns the last element of this list.

3. Delete element

removeFirst(); Removes and returns the first element of this list.

removeLast(); Removes and returns the last element of this list.

Set: duplicate elements are not allowed; The method is the same as that of collection; There is only one set fetch method: iterator

|--HashSet: hash table structure

|--Linkedhashset: hash table + linked list structure. Order can be achieved

This class implements the set interface and is supported by a hash table (actually a HashMap instance). It does not guarantee the iterative order of set; in particular, it does not guarantee that the order is constant. This class allows the use of null elements.

How to maintain element uniqueness?

Element must override hashcode () and equals () methods;

The hashcode () method is used to determine the hash value according to the characteristics of the element itself; The equals () method is overridden to resolve hash value conflicts (the equals () method is required only when the hashcode () method is consistent).

|--TreeSet: you can sort elements; Asynchronous, binary tree data structure

Sorting method 1: elements need to have comparison function Therefore, the element needs to implement the comparable interface and override the CompareTo method

How to ensure element uniqueness?

The reference is whether the return value of the comparison method is 0; If yes, it is a duplicate element and will not be stored.

There is also a situation in the requirements that the comparison function of elements is not required, that is, you do not want to sort elements according to the natural sorting method, but according to the user-defined sorting method.

Moreover, if the elements stored in TreeSet do not have comparison function, how should they be sorted?

Sorting method 2: implement the comparator interface to override the compare () method. Pass the object of comparator interface as a parameter to the constructor of TreeSet collection.

The comparator is flexible. Natural sorting is usually used as the default sorting of elements. Generally, it is not recommended to change it. To customize the sorting method, sorting method 2 is recommended.

Collection skills:

JDK1. 2. The common subclass objects in the collection framework appear later, and the existing laws. The prefix name is the data structure name, and the suffix name is the system name

|--List

ArrayList: array structure; When you see the array structure, you need to know that the query is fast. When you see the list, you know that it can be repeated. You can add, delete, modify and query.

LinkedList: linked list structure, fast addition and deletion. Unique method: xxxfirst xxxlast addxxx getxxx removexxx

|--Set

HashSet: hash table. The query speed is faster. When you see this, you need to think that the element must override the hashcode () method and the equals () method. Order is not guaranteed. When you see a set, you need to know that elements cannot be repeated.

Linkedhashset: linked list + hash table. Because of the linked list, order can be realized.

TreeSet: binary tree, which can be sorted. When you see this collection, you should think of two comparison methods: (1) one is natural sorting comparable (2) the other is user-defined comparator comparator

Map set: double column set, storing one pair at a time and key value pairs; To ensure the uniqueness of the key, the duplicate key overwrites the value of the previous key.

Common functions

1. Add

V put(K key,V value); Associates the specified value with the specified key in this mapping.

putAll(Map
map); Copies all mapping relationships from the specified mapping to this mapping.

2. Delete

void clear(); Remove all mapping relationships from this mapping.

v remove(key); If there is a mapping relationship for a key, remove it from this mapping.

3. Judgment

boolean containsKey(Object o); Returns true if the mapping contains a mapping relationship for the specified key.

boolean containsValue(Object o); Returns true if the mapping maps one or more keys to the specified value.

boolean isEmpty(); Returns true if the mapping does not contain a key value mapping relationship.

4. Obtain

V get(key); Returns the value mapped by the specified key; Null if the mapping does not contain a mapping relationship for the key.

int size(); Returns the number of key value mapping relationships in this mapping.

Map

|--Hashtable: the hash table structure is synchronous, and null keys and null values are not allowed;.

|--HashMap: hash table structure. It is asynchronous. Null keys and null values are allowed;.

|--Treemap: binary tree structure, which is asynchronous. You can sort the keys in the map collection

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>