Java-linked list source code principle analysis, and build a queue through the consumer list, java-linked list

Source: Internet
Author: User

Java-linked list source code principle analysis, and build a queue through the consumer list, java-linked list

Here we will introduce the simplest linked list listing;

Let's take a 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 add principle is:

1. first obtain the last node of the linked list.

2. Insert the new node to the last node.

3. The last attribute of the vertex list points to the last node again.

4. If this node is the first node and there is no node before, point the first attribute of the previous list to the new node. If not, point the next attribute of the previous node to this node.

 

Use the producer list to create a first-in-first-out queue:

Offer () method: Use the add () method to insert nodes at the end.

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

Poll () method: removes the queue from the table header.

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

 

Use the shortlist to construct a post-import/export queue:

Push () method: insert the node to 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: removes the queue from the table header.

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;    }

 

Note that the synchronized list is thread unsafe. To ensure thread security, use synchronized or vector or java. util. concurrent.

To avoid ConcurrentModificationException when the thread needs to traverse the List, there are three solutions.

1. Use synchronized to lock the traversal List;

2. Use the CopyOnWriteArrayList in the java. util. concurrent package. The List copy is actually used every time you use the List.

3. Use the foreach method in JDK 8. However, this method only accepts lambda expressions.

List. forEach (item-> {System. out. println ("traversal element:" + item); try {Thread. sleep (1000);} catch (InterruptedException e) {e. printStackTrace ();}});

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.