JAVA learning summary - common data structures

The collection framework in java is actually the encapsulation of the implementation of the data structure;

Reference: Ren Xiaolong's teaching video

 

1. What is a data structure?

A data structure is the way a computer stores and organizes data;

A data structure is a collection of data elements that have one or more specific relationships to each other;

Often, a well-chosen data structure can lead to higher operational or storage efficiency,

Data structures are often associated with efficient retrieval algorithms and indexing techniques;

 

2, the basic functions of the data structure

Add (Create) Delete (Delete) Change (Update) Check (Read)

 

3, common data structures

3.1,Array Array;

Arrays are the simplest data structure; they are used to hold collections of the same data type .

Analyze array performance from CRUD:

Addition : It is very convenient to add elements in the last position of the array, but it is very troublesome if you want to add elements in the first position, all the following elements must be moved backward as a whole, and the capacity must be expanded if the capacity is not enough;

Delete: It is very convenient to delete the last element of the array, but it is very troublesome to delete the element in the first position, and all the following elements must be moved forward as a whole;

Change: Modify the element with the specified subscript as long as you operate it once;

Search: If you query the element with the specified subscript, you only need to operate it once. If you query the subscript of the specified element, you need to use a linear search (find one by one),

To sum up: the performance of changing and checking the array is relatively high, and the performance of adding and deleting is relatively low;

 

3.2 Linked List; 

 A linked list represents the relationship between the previous node and the next node by reference;

   1"Single-point linked list // can only be traversed from the beginning to the end / can only be traversed from the end to the beginning

The next node is stored by next, and Node next represents the next node;

 

   2" two-way list // can be traversed from the beginning to the end, and can also be traversed from the end to the beginning

The previous node is represented by prev, and Node prev is the previous node;

 

Analyze the performance of linked lists from CRUD:

Added: Doubly linked list can directly get the first node and the last node. If the new element is in the first position or the last position, the operation is only once;

Delete: delete the first element or the last element only once;

Change: There is no concept of subscript, and it needs to be traversed;

Check: There is no concept of subscript, and it needs to be traversed;

To sum up: the addition and deletion performance of the linked list is high, and the performance of changing and checking is low;

 

 

 

3.3 Stack Stack;

It is a linear table with limited operation, last in first out (LIFO);

Only allows adding and removing elements from one end of the list, called the top of the stack, and the other end of the list, called the bottom of the stack

Adding an element to a stack, also known as push or push or push, is to place the new element on top of the top element of the stack, making it the new top element of the stack;

Deleting an element from a stack, also known as popping, deletes the top element of the stack, making its adjacent elements the top element of the stack;

The stack is implemented based on arrays, the element with subscript 0 is the bottom element of the stack, and the last element is the top element of the stack;

 

3.4 Queue Queue;

A queue is a special kind of linear table. The special feature is that only the front end of the table is allowed to delete operations, and the back end of the table is allowed to add operations.

Like a stack, a queue is a restricted linear list

The end that performs the insertion operation is called the tail of the queue, and the end that performs the deletion operation is called the head of the queue;

One-way queue: First-in, first- out (FIFO) can only insert data from the end of the queue, and can only delete data from the head of the queue;

Two-way queue: data can be inserted from the tail/head of the queue, and data can be deleted from the head/tail of the queue;

 

3.5 Hash table Hash

In a general array, the subscript position of the element in the array is random, and there is an uncertain relationship between the value of the element and the position of the element;

Therefore, when the array is searching for a value, it is necessary to compare the search value with a series of elements; the query efficiency at this time depends on the number of comparisons performed in the search process;

If the value of the element (value) and the subscript (index) in the array have a definite correspondence (hash),

Formula: index = hash(value);

Such an array is called a hash table, and the biggest use of the hash table is to provide the efficiency of finding data;

In general, the hash code (hashCode) is not used as the subscript of the array element, because the hash code is large and easy to cross the boundary; the mapping relationship between the hash code and the subscript can be made,

 

Arrays will record the order in which elements are added, and allow elements to be repeated;

The hash table does not record the order in which the elements are added (the hash algorithm is sorted so that one corresponds to one), and repetition is not allowed. The reason is: if the elements are repeated, the hash code values ​​are equal, and the subscripts are equal.

 

Guess you like

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