亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home Java Javagetting Started What are the commonly used data structures in Java?

What are the commonly used data structures in Java?

Apr 14, 2021 pm 04:52 PM
java data structure

Java data structures include: 1. Array; 2. Linked list, a recursive data structure; 3. Stack, which stores data according to the principles of "last in, first out" and "first in, last out"; 4. Queue; 5. Tree, which is a collection of hierarchical relationships composed of n (n>0) limited nodes; 6. Heap; 7. Graph; 8. Hash table.

What are the commonly used data structures in Java?

The operating environment of this tutorial: windows7 system, java8 version, DELL G3 computer.

Common Java Data Structures

What are the differences between these 8 data structures?

①, Array

Advantages:

  • Querying elements by index is very fast;

  • It is also very convenient to traverse the array by index.

Disadvantages:

  • The size of the array is determined after creation and cannot be expanded;

  • Arrays can only store one type of data;

The operations of adding and deleting elements are time-consuming because other elements need to be moved.

②. Linked list

  • The book "Algorithm (4th Edition)" defines a linked list like this:

    The linked list is a recursive data structure. It is either empty (null) or a reference to a node (node). The node also has an element and a pointer to another linked list. citation.

    Java's LinkedList class can express the structure of a linked list very vividly in the form of code:

public class LinkedList<E> {
    transient Node<E> first;
    transient Node<E> last;

    private static class Node<E> {
        E item;
        Node<E> next;
        Node<E> prev;

        Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }
    }
}
  • This is a This is a doubly linked list. The current element item has both prev and next, but the prev of first is null and the next of last is null. If it is a one-way linked list, there is only next and no prev.

Since the linked list does not need to be stored in a sequential manner, the linked list can achieve O(1) time complexity when inserting and deleting (just need to re- Just point to the reference, no need to move other elements like an array). In addition, the linked list also overcomes the shortcoming of the array that the data size must be known in advance, thus enabling flexible dynamic memory management.

Advantages:

  • No need to initialize capacity;

  • You can add any element;

  • Only the reference needs to be updated when inserting and deleting.

Disadvantages:

  • contains a large number of references and takes up a large amount of memory space;

  • Search The elements need to traverse the entire linked list, which is time-consuming.

③、Stack

The stack is like a bucket, the bottom is sealed and the top is open, water can You can go in and out. Friends who have used buckets should understand this principle: the water that goes in first is at the bottom of the bucket, and the water that goes in later is at the top of the bucket; the water that goes in last is poured out first, and the water that goes in first is poured out last.

Similarly, the stack stores data according to the principles of "last in, first out" and "first in, last out". The data inserted first is pushed to the bottom of the stack, and the data inserted later is on the top of the stack. The data is read out. time, read sequentially starting from the top of the stack.

④、Queue

The queue is like a section of water pipe, with both ends open. Water goes in at one end and comes out at the other end. The water that goes in first comes out first, and the water that goes in last comes out last.

Somewhat different from the water pipe, the queue will define two ends, one end is called the head of the queue, and the other end is called the tail of the queue. The head of the queue only allows deletion operations (dequeuing), and the tail of the queue only allows insertion operations (enqueueing).

⑤. Tree

The tree is a typical nonlinear structure, which is composed of n A set of hierarchical relationships composed of (n>0) limited nodes.

The reason why it is called "tree" is because this data structure looks like an upside-down tree, except that the root is at the top and the leaves are at the bottom. The tree data structure has the following characteristics:

  • Each node has only limited child nodes or no child nodes;

  • There is no parent node The node is called the root node;

  • Each non-root node has and has only one parent node;

  • Except for the root node, each child node Can be divided into multiple disjoint subtrees.

The following figure shows some terms for trees:

Based on the characteristics of the binary search tree, its advantage over other data structures is that the time complexity of search and insertion is low, which is O(logn). If we want to find 5 elements from the above picture, we start from the root node 7. If we find 5, it must be on the left of 7. If we find 4, then 5 must be on the right of 4. If we find 6, then 5 must be on the left of 6. Side, found.

Ideally, by finding nodes through BST, the number of nodes that need to be checked can be halved.

Balanced binary tree: A binary tree if and only if the height difference between the two subtrees of any node is not greater than 1. The highly balanced binary tree proposed by the former Soviet mathematicians Adelse-Velskil and Landis in 1962 is also called an AVL tree according to the scientists' English name.

The balanced binary tree is essentially a binary search tree. However, in order to limit the height difference between the left and right subtrees and avoid situations such as tilted trees that tend to evolve toward linear structures, each of the binary search trees is The left and right subtrees of the node are restricted. The height difference between the left and right subtrees is called the balance factor. The absolute value of the balance factor of each node in the tree is not greater than 1.

The difficulty of balancing a binary tree is how to maintain left-right balance through left- or right-hand rotation when nodes are deleted or added.

The most common balanced binary tree in Java is the red-black tree. The nodes are red or black. The balance of the binary tree is maintained through color constraints:

1) Each node can only be Red or black

2) The root node is black

3) Each leaf node (NIL node, empty node) is black.

4) If a node is red, both of its child nodes are black. That is to say, two adjacent red nodes cannot appear on a path.

5) All paths from any node to each of its leaves contain the same number of black nodes.

⑥, Heap

The heap can be regarded as an array object of a tree, with The following characteristics:

  • The value of a node in the heap is always not greater than or not less than the value of its parent node;

  • The heap is always A complete binary tree.

The heap with the largest root node is called the maximum heap or large root heap, and the heap with the smallest root node is called the minimum heap or small root heap.

In a linear structure, a unique linear relationship is satisfied between data elements, and each data element (except the first and last) Each has a unique "predecessor" and "successor";

In the tree structure, there is an obvious hierarchical relationship between data elements, and each data element is only related to one element in the previous layer (parent node) and multiple elements (child nodes) of the next layer are related;

In the graph structure, the relationship between nodes is arbitrary, and any two data elements in the graph may be related. .

⑧, Hash table

Hash table, also called hash table, is a type of hash table that can pass key code values (key-value) direct access data structure, its biggest feature is that it can quickly implement search, insertion and deletion.

The biggest feature of an array is that it is easy to search, but difficult to insert and delete; on the contrary, the linked list is difficult to search, but easy to insert and delete. Hash tables perfectly combine the advantages of both. Java's HashMap also adds the advantages of trees on this basis.

The hash function plays a very key role in the hash table. It can transform input of any length into a fixed length Output, the output is the hash value. The hash function makes the access process of a data sequence faster and more efficient. Through the hash function, the data elements can be quickly located.

If the keyword is k, its value is stored in the storage location of hash(k). Therefore, the value corresponding to k can be obtained directly without traversal.

For any two different data blocks, the possibility of having the same hash value is extremely small. That is to say, for a given data block, it is extremely difficult to find a data block with the same hash value. Furthermore, for a data block, even if only one bit of it is changed, the change in the hash value will be very large - this is the value of Hash!

Although the possibility is extremely small, it will still happen. If a hash conflict occurs, Java's HashMap will add a linked list to the same position in the array. If the length of the linked list is greater than 8, it will be converted into red and black. The tree is processed - this is the so-called zipper method (array linked list).

For more programming related knowledge, please visit: Programming Video! !

The above is the detailed content of What are the commonly used data structures in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
VSCode settings.json location VSCode settings.json location Aug 01, 2025 am 06:12 AM

The settings.json file is located in the user-level or workspace-level path and is used to customize VSCode settings. 1. User-level path: Windows is C:\Users\\AppData\Roaming\Code\User\settings.json, macOS is /Users//Library/ApplicationSupport/Code/User/settings.json, Linux is /home//.config/Code/User/settings.json; 2. Workspace-level path: .vscode/settings in the project root directory

How to handle transactions in Java with JDBC? How to handle transactions in Java with JDBC? Aug 02, 2025 pm 12:29 PM

To correctly handle JDBC transactions, you must first turn off the automatic commit mode, then perform multiple operations, and finally commit or rollback according to the results; 1. Call conn.setAutoCommit(false) to start the transaction; 2. Execute multiple SQL operations, such as INSERT and UPDATE; 3. Call conn.commit() if all operations are successful, and call conn.rollback() if an exception occurs to ensure data consistency; at the same time, try-with-resources should be used to manage resources, properly handle exceptions and close connections to avoid connection leakage; in addition, it is recommended to use connection pools and set save points to achieve partial rollback, and keep transactions as short as possible to improve performance.

Mastering Dependency Injection in Java with Spring and Guice Mastering Dependency Injection in Java with Spring and Guice Aug 01, 2025 am 05:53 AM

DependencyInjection(DI)isadesignpatternwhereobjectsreceivedependenciesexternally,promotingloosecouplingandeasiertestingthroughconstructor,setter,orfieldinjection.2.SpringFrameworkusesannotationslike@Component,@Service,and@AutowiredwithJava-basedconfi

python itertools combinations example python itertools combinations example Jul 31, 2025 am 09:53 AM

itertools.combinations is used to generate all non-repetitive combinations (order irrelevant) that selects a specified number of elements from the iterable object. Its usage includes: 1. Select 2 element combinations from the list, such as ('A','B'), ('A','C'), etc., to avoid repeated order; 2. Take 3 character combinations of strings, such as "abc" and "abd", which are suitable for subsequence generation; 3. Find the combinations where the sum of two numbers is equal to the target value, such as 1 5=6, simplify the double loop logic; the difference between combinations and arrangement lies in whether the order is important, combinations regard AB and BA as the same, while permutations are regarded as different;

Troubleshooting Common Java `OutOfMemoryError` Scenarios Troubleshooting Common Java `OutOfMemoryError` Scenarios Jul 31, 2025 am 09:07 AM

java.lang.OutOfMemoryError: Javaheapspace indicates insufficient heap memory, and needs to check the processing of large objects, memory leaks and heap settings, and locate and optimize the code through the heap dump analysis tool; 2. Metaspace errors are common in dynamic class generation or hot deployment due to excessive class metadata, and MaxMetaspaceSize should be restricted and class loading should be optimized; 3. Unabletocreatenewnativethread due to exhausting system thread resources, it is necessary to check the number of threads, use thread pools, and adjust the stack size; 4. GCoverheadlimitexceeded means that GC is frequent but has less recycling, and GC logs should be analyzed and optimized.

python pytest fixture example python pytest fixture example Jul 31, 2025 am 09:35 AM

fixture is a function used to provide preset environment or data for tests. 1. Use the @pytest.fixture decorator to define fixture; 2. Inject fixture in parameter form in the test function; 3. Execute setup before yield, and then teardown; 4. Control scope through scope parameters, such as function, module, etc.; 5. Place the shared fixture in conftest.py to achieve cross-file sharing, thereby improving the maintainability and reusability of tests.

Understanding the Java Virtual Machine (JVM) Internals Understanding the Java Virtual Machine (JVM) Internals Aug 01, 2025 am 06:31 AM

TheJVMenablesJava’s"writeonce,runanywhere"capabilitybyexecutingbytecodethroughfourmaincomponents:1.TheClassLoaderSubsystemloads,links,andinitializes.classfilesusingbootstrap,extension,andapplicationclassloaders,ensuringsecureandlazyclassloa

How to work with Calendar in Java? How to work with Calendar in Java? Aug 02, 2025 am 02:38 AM

Use classes in the java.time package to replace the old Date and Calendar classes; 2. Get the current date and time through LocalDate, LocalDateTime and LocalTime; 3. Create a specific date and time using the of() method; 4. Use the plus/minus method to immutably increase and decrease the time; 5. Use ZonedDateTime and ZoneId to process the time zone; 6. Format and parse date strings through DateTimeFormatter; 7. Use Instant to be compatible with the old date types when necessary; date processing in modern Java should give priority to using java.timeAPI, which provides clear, immutable and linear

See all articles