search
Home类库下载java类库Java-LinkedList source code principle analysis, and constructing a queue through LinkedList

Here we introduce the simplest linked list, LinkedList;

Look at the add() method:

public boolean add(E e) {
        linkLast(e);        return true;
    }
void linkLast(E e) {
        final Node<E> l = last;
        final Node<E> newNode = new Node<>(l, e, null);
        last = newNode;
        if (l == null)
            first = newNode;
        else
            l.next = newNode;
        size++;
        modCount++;
    }

The principle of add is:

1. First get the last node of the linked list.

2. Insert the new node after the last node.

3.The last attribute of linkedList redirects to the last node.

4. If this node is the first node and there is no previous node, then point the first attribute of the linkedList to the new node; if not, point the next attribute of the previous node to the node.

Use LinkedList to build a first-in-first-out queue:

offer() method to join the queue: use the add() method to insert the node at the end.

public boolean offer(E e) {
        return add(e);
    }

poll() method dequeue: remove the queue from the head of the linked list

public E poll() {
        final Node<E> f = first;
        return (f == null) ? null : unlinkFirst(f);
    }

Use LinkedList to build a last-in-first-out queue:

push() method enqueue: insert the node at the first

public void addFirst(E e) {
        linkFirst(e);
    }

private void linkFirst(E e) {
        final Node<E> f = first;
        final Node<E> newNode = new Node<>(null, e, f);
        first = newNode;
        if (f == null)
            last = newNode;
        else
            f.prev = newNode;
        size++;
        modCount++;
    }

pop() method dequeue : Remove from the queue starting from the head of the linked list

public E pop() {
        return removeFirst();
    }

public E removeFirst() {
        final Node<E> f = first;
        if (f == null)
            throw new NoSuchElementException();
        return unlinkFirst(f);
    }

private E unlinkFirst(Node<E> f) {
        // assert f == first && f != null;
        final E element = f.item;
        final Node<E> next = f.next;
        f.item = null;
        f.next = null; // help GC
        first = next;
        if (next == null)
            last = null;
        else
            next.prev = null;
        size--;
        modCount++;
        return element;
    }

The last thing to note is: LinkedList is thread-unsafe. If you need thread safety, please use synchronized locking, or use vector, or use the java.util.concurrent package.

If you need to avoid ConcurrentModificationException when a thread traverses the List, there are 3 solutions.

1. Use synchronized locking when traversing the List;

2. Use CopyOnWriteArrayList under the java.util.concurrent package. Every time you use the List, you are actually using a copy of the List.

3. Use the foreach method in Jdk8, but this method only accepts lambda expressions

list.forEach(item -> {
                    System.out.println("遍历元素:" + item);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                });


Statement
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

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.

Undress AI Tool

Undress AI Tool

Undress images for free

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

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment