LinkedList is a doubly linked list that inherits AbstractSequentialList. It can also be operated as a stack, queue or double-ended queue.
LinkedList implements the List interface and can perform queue operations on it.
LinkedList implements the Seque interface and can use LinkedList as a double-ended queue.
LinkedList implements Cloneable, covers the clone function, and can be cloned.
LinkedList implements Serializable and can be serialized.
LinkedList is not thread-safe.
LinkedList sample program:
public class Hello { public static void main(String[] args) { testLinkedListAPIs() ; useLinkedListAsLIFO(); useLinkedListAsFIFO(); } private static void testLinkedListAPIs() { String val = null; LinkedList llist = new LinkedList(); //添加是哪个元素 llist.add("1"); llist.add("2"); llist.add("3"); llist.add(1, "4");//在第一个元素后面插入4 System.out.println("\nTest \"addFirst(), removeFirst(), getFirst()\""); llist.addFirst("10");//将10插入并作为第一个元素 System.out.println("llist:"+llist); System.out.println("llist.removeFirst():"+llist.removeFirst());//删除掉第一个元素 System.out.println("llist:"+llist); System.out.println("llist.getFirst():"+llist.getFirst());//获得第一个元素 System.out.println("\nTest \"offerFirst(), pollFirst(), peekFirst()\""); llist.offerFirst("10");//添加第一个元素 System.out.println("llist:"+llist); System.out.println("llist.pollFirst():"+llist.pollFirst());//去掉第一个元素 System.out.println("llist:"+llist); System.out.println("llist.peekFirst():"+llist.peekFirst());//读取第一个元素 System.out.println("\nTest \"addLast(), removeLast(), getLast()\""); llist.addLast("20");//链尾添加元素20 System.out.println("llist:"+llist); System.out.println("llist.removeLast():"+llist.removeLast());//删掉链尾元素20 System.out.println("llist:"+llist); System.out.println("llist.getLast():"+llist.getLast());//读取链尾元素 System.out.println("\nTest \"offerLast(), pollLast(), peekLast()\""); llist.offerLast("20");//添加链尾元素20 System.out.println("llist:"+llist); System.out.println("llist.pollLast():"+llist.pollLast());//删掉链尾元素20 System.out.println("llist:"+llist); System.out.println("llist.peekLast():"+llist.peekLast());//读取链尾元素 llist.set(2, "300");//替换第三个元素 System.out.println("\nget(3):"+llist.get(2));//获得第三个元素 String[] arr = (String[])llist.toArray(new String[0]);//得到数组 for (String str:arr) { System.out.println("str:"+str); System.out.println("size:"+llist.size()); llist.clear(); System.out.println("isEmpty():"+llist.isEmpty()+"\n"); } } private static void useLinkedListAsLIFO() { System.out.println("\nuseLinkedListAsLIFO"); LinkedList stack = new LinkedList(); //类似于栈输入 stack.push("1"); stack.push("2"); stack.push("3"); stack.push("4"); System.out.println("stack:"+stack); System.out.println("stack.pop():"+stack.pop());//出栈 System.out.println("stack.peek():"+stack.peek());//只输出栈顶元素并不出栈 System.out.println("stack:"+stack); } private static void useLinkedListAsFIFO() { System.out.println("\nuseLinkedListAsFIFO"); LinkedList queue = new LinkedList(); //类似于队列,入队 queue.add("10"); queue.add("20"); queue.add("30"); queue.add("40"); System.out.println("queue:"+queue); System.out.println("queue.remove():"+queue.remove());//队列出队 System.out.println("queue.element():"+queue.element());//读取队头,并不删除元素 System.out.println("queue:"+queue); } }
Output result:
Test "offerLast(), pollLast(), peekLast()" llist:[1, 4, 2, 3, 20] llist.pollLast():20 llist:[1, 4, 2, 3] llist.peekLast():3 get(3):300 str:1 size:4 isEmpty():true str:4 size:0 isEmpty():true str:300 size:0 isEmpty():true str:3 size:0 isEmpty():true useLinkedListAsLIFO stack:[4, 3, 2, 1] stack.pop():4 stack.peek():3 stack:[3, 2, 1] useLinkedListAsFIFO queue:[10, 20, 30, 40] queue.remove():10 queue.element():20 queue:[20, 30, 40]
LinkedList source code:
public class LinkedList<e> extends AbstractSequentialList<e> implements List<e>, Deque<e>, Cloneable, java.io.Serializable { transient int size = 0; //其实大小为0 transient Node<e> first; //第一个节点 transient Node<e> last; //最后一个节点 public LinkedList() { //构造一个空LinkedList } public LinkedList(Collection extends E> c) { //构造一个带有输入集合的 this(); addAll(c); } 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++; } 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++; } void linkBefore(E e, Node<e> succ) { //在节点succ前插入一个e // assert succ != null; final Node<e> pred = succ.prev; final Node<e> newNode = new Node(pred, e, succ); succ.prev = newNode; if (pred == null) first = newNode; else pred.next = newNode; size++; modCount++; } //不再链接第一个非空的节点 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; } //删掉最后一个节点 private E unlinkLast(Node<e> l) { // assert l == last && l != null; final E element = l.item; final Node<e> prev = l.prev; l.item = null; l.prev = null; // help GC last = prev; if (prev == null) first = null; else prev.next = null; size--; modCount++; return element; } //删掉节点X E unlink(Node<e> x) { // assert x != null; final E element = x.item; final Node<e> next = x.next; final Node<e> prev = x.prev; if (prev == null) { first = next; } else { prev.next = next; x.prev = null; } if (next == null) { last = prev; } else { next.prev = prev; x.next = null; } x.item = null; size--; modCount++; return element; } //获得第一个节点 public E getFirst() { final Node<e> f = first; if (f == null) throw new NoSuchElementException(); return f.item; } //获得最后一个节点 public E getLast() { final Node<e> l = last; if (l == null) throw new NoSuchElementException(); return l.item; } //删除第一个节点 public E removeFirst() { final Node<e> f = first; if (f == null) throw new NoSuchElementException(); return unlinkFirst(f); } //删除最后一个节点 public E removeLast() { final Node<e> l = last; if (l == null) throw new NoSuchElementException(); return unlinkLast(l); } //在头节点插入E public void addFirst(E e) { linkFirst(e); } //在尾节点插入E public void addLast(E e) { linkLast(e); } //是否包含某个对象 public boolean contains(Object o) { return indexOf(o) != -1; } //链表长度 public int size() { return size; } //在链表中添加节点 public boolean add(E e) { linkLast(e); return true; } //删掉某个节点 public boolean remove(Object o) { if (o == null) { for (Node<e> x = first; x != null; x = x.next) { if (x.item == null) { unlink(x); return true; } } } else { for (Node<e> x = first; x != null; x = x.next) { if (o.equals(x.item)) { unlink(x); return true; } } } return false; } //在链表尾追加集合 public boolean addAll(Collection extends E> c) { return addAll(size, c); } //在某个节点之后追加集合 public boolean addAll(int index, Collection extends E> c) { checkPositionIndex(index); Object[] a = c.toArray(); int numNew = a.length; if (numNew == 0) return false; Node<e> pred, succ; if (index == size) { succ = null; pred = last; } else { succ = node(index); pred = succ.prev; } for (Object o : a) { @SuppressWarnings("unchecked") E e = (E) o; Node<e> newNode = new Node(pred, e, null); if (pred == null) first = newNode; else pred.next = newNode; pred = newNode; } if (succ == null) { last = pred; } else { pred.next = succ; succ.prev = pred; } size += numNew; modCount++; return true; } //清空链表 public void clear() { for (Node<e> x = first; x != null; ) { Node<e> next = x.next; x.item = null; x.next = null; x.prev = null; x = next; } first = last = null; size = 0; modCount++; } //获得第几个节点 public E get(int index) { checkElementIndex(index); return node(index).item; } //对某个节点修改 public E set(int index, E element) { checkElementIndex(index); Node<e> x = node(index); E oldVal = x.item; x.item = element; return oldVal; } //在index节点之前插入一个节点element public void add(int index, E element) { checkPositionIndex(index); if (index == size) linkLast(element); else linkBefore(element, node(index)); } //删除掉下标为index的节点 public E remove(int index) { checkElementIndex(index); return unlink(node(index)); } //测试此index下是否有节点 private boolean isElementIndex(int index) { return index >= 0 && index = 0 && index node(int index) { // assert isElementIndex(index); if (index > 1)) { Node<e> x = first; for (int i = 0; i x = last; for (int i = size - 1; i > index; i--) x = x.prev; return x; } } //查找是否存在节点并返回下标,不存在返回-1 public int indexOf(Object o) { int index = 0; if (o == null) { for (Node<e> x = first; x != null; x = x.next) { if (x.item == null) return index; index++; } } else { for (Node<e> x = first; x != null; x = x.next) { if (o.equals(x.item)) return index; index++; } } return -1; } //返回最后一个相同的节点的下标 public int lastIndexOf(Object o) { int index = size; if (o == null) { for (Node<e> x = last; x != null; x = x.prev) { index--; if (x.item == null) return index; } } else { for (Node<e> x = last; x != null; x = x.prev) { index--; if (o.equals(x.item)) return index; } } return -1; } //返回表头数据并不删除 public E peek() { final Node<e> f = first; return (f == null) ? null : f.item; } //返回表头数据 public E element() { return getFirst(); } //获得表头数据,并删除表头 public E poll() { final Node<e> f = first; return (f == null) ? null : unlinkFirst(f); } //删除表头 public E remove() { return removeFirst(); } //在表尾添加数据 public boolean offer(E e) { return add(e); } //在表头添加数据 public boolean offerFirst(E e) { addFirst(e); return true; } //在表尾添加数据 public boolean offerLast(E e) { addLast(e); return true; } //获得第一个数据,并不删除 public E peekFirst() { final Node<e> f = first; return (f == null) ? null : f.item; } //获得最后一个数据,并不删除 public E peekLast() { final Node<e> l = last; return (l == null) ? null : l.item; } //获得第一个节点并删除 public E pollFirst() { final Node<e> f = first; return (f == null) ? null : unlinkFirst(f); } //获得最后一个节点并删除 public E pollLast() { final Node<e> l = last; return (l == null) ? null : unlinkLast(l); } //入栈 public void push(E e) { addFirst(e); } //出栈 public E pop() { return removeFirst(); } //删除第一次出现的对象 public boolean removeFirstOccurrence(Object o) { return remove(o); } //删除最后一次出现的对象 public boolean removeLastOccurrence(Object o) { if (o == null) { for (Node<e> x = last; x != null; x = x.prev) { if (x.item == null) { unlink(x); return true; } } } else { for (Node<e> x = last; x != null; x = x.prev) { if (o.equals(x.item)) { unlink(x); return true; } } } return false; } //迭代器 public ListIterator<e> listIterator(int index) { checkPositionIndex(index); return new ListItr(index); } private class ListItr implements ListIterator<e> { private Node<e> lastReturned; private Node<e> next; private int nextIndex; private int expectedModCount = modCount; ListItr(int index) { // assert isPositionIndex(index); next = (index == size) ? null : node(index); nextIndex = index; } public boolean hasNext() { return nextIndex 0; } public E previous() { checkForComodification(); if (!hasPrevious()) throw new NoSuchElementException(); lastReturned = next = (next == null) ? last : next.prev; nextIndex--; return lastReturned.item; } public int nextIndex() { return nextIndex; } public int previousIndex() { return nextIndex - 1; } public void remove() { checkForComodification(); if (lastReturned == null) throw new IllegalStateException(); Node<e> lastNext = lastReturned.next; unlink(lastReturned); if (next == lastReturned) next = lastNext; else nextIndex--; lastReturned = null; expectedModCount++; } public void set(E e) { if (lastReturned == null) throw new IllegalStateException(); checkForComodification(); lastReturned.item = e; } public void add(E e) { checkForComodification(); lastReturned = null; if (next == null) linkLast(e); else linkBefore(e, next); nextIndex++; expectedModCount++; } public void forEachRemaining(Consumer super E> action) { Objects.requireNonNull(action); while (modCount == expectedModCount && nextIndex { 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; } } /** * @since 1.6 */ public Iterator<e> descendingIterator() { return new DescendingIterator(); } /** * Adapter to provide descending iterators via ListItr.previous */ private class DescendingIterator implements Iterator<e> { private final ListItr itr = new ListItr(size()); public boolean hasNext() { return itr.hasPrevious(); } public E next() { return itr.previous(); } public void remove() { itr.remove(); } } @SuppressWarnings("unchecked") private LinkedList<e> superClone() { try { return (LinkedList<e>) super.clone(); } catch (CloneNotSupportedException e) { throw new InternalError(e); } } public Object clone() { LinkedList<e> clone = superClone(); // Put clone into "virgin" state clone.first = clone.last = null; clone.size = 0; clone.modCount = 0; // Initialize clone with our elements for (Node<e> x = first; x != null; x = x.next) clone.add(x.item); return clone; } //生成对象数组 public Object[] toArray() { Object[] result = new Object[size]; int i = 0; for (Node<e> x = first; x != null; x = x.next) result[i++] = x.item; return result; } //泛型数组 @SuppressWarnings("unchecked") public <t> T[] toArray(T[] a) { if (a.length x = first; x != null; x = x.next) result[i++] = x.item; if (a.length > size) a[size] = null; return a; } private static final long serialVersionUID = 876323262645176354L; //序列化写对象 private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { // Write out any hidden serialization magic s.defaultWriteObject(); // Write out size s.writeInt(size); // Write out all elements in the proper order. for (Node<e> x = first; x != null; x = x.next) s.writeObject(x.item); } //序列化读对象 @SuppressWarnings("unchecked") private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { // Read in any hidden serialization magic s.defaultReadObject(); // Read in size int size = s.readInt(); // Read in all elements in the proper order. for (int i = 0; i spliterator() { return new LLSpliterator<e>(this, -1, 0); } /** A customized variant of Spliterators.IteratorSpliterator */ static final class LLSpliterator<e> implements Spliterator<e> { static final int BATCH_UNIT = 1 list; // null OK unless traversed Node<e> current; // current node; null until initialized int est; // size estimate; -1 until first needed int expectedModCount; // initialized when est set int batch; // batch size for splits LLSpliterator(LinkedList<e> list, int est, int expectedModCount) { this.list = list; this.est = est; this.expectedModCount = expectedModCount; } final int getEst() { int s; // force initialization final LinkedList<e> lst; if ((s = est) trySplit() { Node<e> p; int s = getEst(); if (s > 1 && (p = current) != null) { int n = batch + BATCH_UNIT; if (n > s) n = s; if (n > MAX_BATCH) n = MAX_BATCH; Object[] a = new Object[n]; int j = 0; do { a[j++] = p.item; } while ((p = p.next) != null && j action) { Node<e> p; int n; if (action == null) throw new NullPointerException(); if ((n = getEst()) > 0 && (p = current) != null) { current = null; est = 0; do { E e = p.item; p = p.next; action.accept(e); } while (p != null && --n > 0); } if (list.modCount != expectedModCount) throw new ConcurrentModificationException(); } public boolean tryAdvance(Consumer super E> action) { Node<e> p; if (action == null) throw new NullPointerException(); if (getEst() > 0 && (p = current) != null) { --est; E e = p.item; current = p.next; action.accept(e); if (list.modCount != expectedModCount) throw new ConcurrentModificationException(); return true; } return false; } public int characteristics() { return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED; } } }</e></e></e></e></e></e></e></e></e></e></t></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e></e>
The above is the detailed content of Detailed explanation of LinkedList in Java collections. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
