View the simple table structure and JAVA data structure from the perspective of java Data Structure
1. Preface:
When we talk about collections in programs, we often come to our minds, such as ArrayList and aggregate list and HashMap. Of course, when talking about ArrayList and rule list, most of us know that ArrayList queries are fast and operations are slow. Slow list query speed, but fast operation speed. But why? Why is it like this? Next, I will take you to understand this question from the perspective of data structure.
2. Data Structure-linear table
Data structures are used by computers to store and organize data.
A Data Structure refers to a set of data elements with one or more specific relationships between each other.
The concept above is as follows: Data Structure = Data Storage + data set.
Linear table: a linear table is a linear structure. It is a finite sequence containing n ≥ 0 knots. For the nodes, there is only one starting node, there is no precursor, but there is a successor node, there is only one terminal node without any successor, but there is a precursor node. Other nodes have only one and only one successor node.
Thread Table A = {A0, A1, A2 ,..... , An-1, An, An + 1}, we call it An-1 as the precursor element of An, and An + 1 as the successor element.
3. Storage Method
We have a simple understanding of the structure of linear tables, so we classify linear tables into linear storage and chain storage based on computer storage.
3.1 linear Storage
Linear storage in linear tables indicates that the elements in our data structure are stored in computer memory in order when they are stored. Typical Example: ArrayList
The design data structure of linear storage must include the following:
I. Start position
II. Array
III. storage location of tablespace Elements
As we can see from the above, when designing a thread table, we maintain the relationship between data structures in the form of arrays, without the need to independently design the maintenance of logical relationships.
In terms of maintaining the logical relationship of arrays, the array itself contains records of element indexes. Index search is faster.
Disadvantages:
Is it because the data structure is maintained by arrays?
First, when adding elements, an issue involved is the automatic expansion policy of the array.
The Linear Linked List of sequential storage is an array copy in the test of automatic expansion.
See ArrayList. Add source code analysis elements
The copy Implementation of the array Arrays. copyOf (elementData, newCapacity) is selected for automatic resizing.
When an array is copied, it involves a large number of copying and migration of elements in the data structure.
The excessive expansion from figure 1 to figure 2 is the process of automatic expansion of the linear table. The steps involved include creating arrays and copying elements, which increases the performance overhead.
Ii. Space fragmentation issues
We are using arrays to open up memory space, but when we have not used the space for opening up arrays, the remaining unused space will be continuously occupied and cannot be released, space is wasted, which is called space fragmentation.
3.2 chain Storage
In a linear table, chained storage means that elements in the data structure are stored randomly in the available area of computer memory when they are stored. In this way, the memory space can be used better. Typical Example: Upload list
Linear table structure of chained storage: the logical relationship between elements is not maintained using arrays during design. We need to design nodes to describe the element relationship in the data structure.
We need to maintain the "Neighbor Relationship" between nodes and use any group of storage unit storage elements, the linear logical relationship between linear tables is directed to the next group of storage units through the pointer field in this group of storage units. The next group of storage units can be placed anywhere.
Refer to the javaslist source code:
We can see from the data structure above that the logical relationship of linear tables stored in chain stores the logical relationship through the pointer domain, so the distribution in memory is a logical association, physically, they are separated.
This design improves memory usage efficiency and avoids space fragmentation. You can maintain the logical relationship on your own. Therefore, logical addition or deletion operations are more efficient.
Today, we will focus on understanding our simple table structure from the perspective of data structure. Have you learned? We know our old friends ArrayList and rule list from different perspectives. Next, we will provide more knowledge about the java backend. Please pay more attention to the JAVA training of Shanghai shangxue and look forward to seeing you next time.