Java LinkedList is an implementation of the List and Deque interfaces. It is one of the frequently used List implementation class.

Java LinkedList是List和Deque接口的实现。 它是常用的List实现类之一。

It extends AbstractSequentialList and implements List and Deque interfaces. It is an ordered collection and supports duplicate elements. It stores elements in Insertion order. It supports adding null elements. It supports index based operations.

它扩展了AbstractSequentialList并实现了List和Deque接口。 它是一个有序的集合,并支持重复的元素。 它按插入顺序存储元素。 它支持添加空元素。 它支持基于索引的操作。

If you want to learn more about List basics, please go through this post: Java List.

如果您想了解有关List基础的更多信息,请阅读这篇文章: Java List 。

发布简要目录 (Post Brief Table of Content)

In this post we are going to this discuss the following concepts.

在这篇文章中,我们将讨论以下概念。

  • Java LinkedListJava LinkedList
  • Java LinkedList Class DiagramJava LinkedList类图
  • Java LinkedList List MethodsJava LinkedList列表方法
  • Java LinkedList Deque MethodsJava LinkedList双端队列方法
  • Java LinkedList Basic ExampleJava LinkedList基本示例
  • Java LinkedList GenericsJava LinkedList泛型
  • Java Array to LinkedListJava数组到LinkedList
  • Java LinkedList to ArrayJava LinkedList到数组
  • Java LinkedList Real-time UsecasesJava LinkedList实时用例
  • Internal Representation of Java LinkedListJava LinkedList的内部表示
  • How Insertion works in Java LinkedList?Java LinkedList中插入如何工作?
  • How Deletion works in Java LinkedList?删除如何在Java LinkedList中工作?
  • Java LinkedList Deque OperationsJava LinkedList双端队列操作
  • Java SE 8: Java LinkedList to StreamJava SE 8:要发送的Java LinkedList
  • Java SE 9 LinkedListJava SE 9链表

Java LinkedList (Java LinkedList)

In this section, we will discuss some of the important points about Java LinkedList:

在本节中,我们将讨论有关Java LinkedList的一些要点:

  • Java LinkedList class is a member of the Java Collections Framework.Java LinkedList类是Java Collections Framework的成员。
  • It is an implementation of the List and Deque interfaces.它是List和Deque接口的实现。
  • Internally, it is an implemented using Doubly Linked List Data Structure.在内部,它是使用双链表数据结构实现的。
  • It supports duplicate elements.它支持重复元素。
  • It stores or maintains it’s elements in Insertion order.它按插入顺序存储或维护其元素。
  • We can add any number of null elements.我们可以添加任意数量的null元素。
  • It is not synchronised that means it is not Thread safe.它不同步,这意味着它不是线程安全的。
  • We can create a synchronised LinkedList using Collections.synchronizedList() method.我们可以使用Collections.synchronizedList()方法创建一个同步的LinkedList。
  • In Java applications, we can use it as a List, stack or queue.在Java应用程序中,我们可以将其用作列表,堆栈或队列。
  • It does not implement RandomAccess interface. So we can access elements in sequential order only. It does not support accessing elements randomly.它没有实现RandomAccess接口。 因此,我们只能按顺序访问元素。 它不支持随机访问元素。
  • When we try to access an element from a LinkedList, searching that element starts from the beginning or end of the LinkedList based on where that elements is available.当我们尝试从LinkedList访问元素时,将根据该元素的可用位置从LinkedList的开头或结尾开始搜索该元素。
  • We can use ListIterator to iterate LinkedList elements.我们可以使用ListIterator来迭代LinkedList元素。
  • From Java SE 8 on-wards, we can convert a LinkedList into a Stream and vice-versa.从Java SE 8开始,我们可以将LinkedList转换为Stream,反之亦然。
  • Java SE 9 is going to add couple of factory methods to create an Immutable LinkedList.Java SE 9将添加一些工厂方法来创建不可变的LinkedList。

Java LinkedList类图 (Java LinkedList Class Diagram)

As we know, Java LinkedList is one the List implementation class. It also implements Deque. As shown in class diagram below, it does NOT extends directly from AbstractList class. It extends AbstractSequentialList class.

众所周知,Java LinkedList是List实现类之一。 它还实现了双端队列。 如下面的类图中所示,它并不直接从AbstractList类扩展。 它扩展了AbstractSequentialList类。

Java LinkedList列表方法 (Java LinkedList List Methods)

In this section we will discuss some of the useful and frequently used Java LinkedList methods.

在本节中,我们将讨论一些有用且经常使用的Java LinkedList方法。

The following methods are inherited from List or Collection interface:

以下方法是从List或Collection接口继承的:

  1. int size(): to get the number of elements in the list.int size():获取列表中元素的数量。
  2. boolean isEmpty(): to check if list is empty or not.boolean isEmpty():检查列表是否为空。
  3. boolean contains(Object o): Returns true if this list contains the specified element.boolean contains(Object o):如果此列表包含指定的元素,则返回true。
  4. Iterator iterator(): Returns an iterator over the elements in this list in proper sequence.Iterator iterator():以适当的顺序返回对该列表中的元素进行迭代的迭代器。
  5. Object[] toArray(): Returns an array containing all of the elements in this list in proper sequence.Object [] toArray():按正确顺序返回包含此列表中所有元素的数组。
  6. boolean add(E e): Appends the specified element to the end of this list.boolean add(E e):将指定的元素追加到此列表的末尾。
  7. boolean remove(Object o): Removes the first occurrence of the specified element from this list.boolean remove(Object o):从此列表中删除第一次出现的指定元素。
  8. boolean retainAll(Collection c): Retains only the elements in this list that are contained in the specified collection.boolean keepAll(Collection c):仅保留此列表中指定集合中包含的元素。
  9. void clear(): Removes all the elements from the list.void clear():从列表中删除所有元素。
  10. E get(int index): Returns the element at the specified position in the list.E get(int index):返回列表中指定位置的元素。
  11. E set(int index, E element): Replaces the element at the specified position in the list with the specified element.E set(int index,E element):将列表中指定位置的元素替换为指定的元素。
  12. ListIterator listIterator(): Returns a list iterator over the elements in the list.ListIterator listIterator():返回列表中元素的列表迭代器。
  13. List subList(int fromIndex, int toIndex): Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa.List subList(int fromIndex,int toIndex):返回此列表中指定的fromIndex(包括)和toIndex(不包括)之间的视图。 返回列表由该列表支持,因此返回列表中的非结构更改会反映在此列表中,反之亦然。

Java LinkedList双端队列方法 (Java LinkedList Deque Methods)

The following methods are specific to LinkedList class which are inherited from Deque interface:

以下方法特定于从Deque接口继承的LinkedList类:

  1. void addFirst(E e): Inserts the specified element at the beginning of this list.void addFirst(E e):在此列表的开头插入指定的元素。
  2. void addLast(E e): Inserts the specified element at the end of this list.addLast(E e):将指定的元素插入此列表的末尾。
  3. E getFirst(): Retrieves, but does not remove, the first element of this list. This method differs from peekFirst only in that it throws an exception if this list is empty.E getFirst():检索但不删除此列表的第一个元素。 此方法与peekFirst的不同之处仅在于,如果此列表为空,它将引发异常。
  4. E getLast(): Retrieves, but does not remove, the last element of this list. This method differs from peekLast only in that it throws an exception if this list is empty.E getLast():检索但不删除此列表的最后一个元素。 此方法与peekLast的不同之处仅在于,如果此列表为空,它将引发异常。
  5. E remvoeFirst(): Removes and returns the first element from this list.E remvoeFirst():从此列表中删除并返回第一个元素。
  6. E removeLast(): Removes and returns the last element from this list.E removeLast():从此列表中删除并返回最后一个元素。
  7. boolean offerFirst(E e): Inserts the specified element at the front of this list.boolean offerFirst(E e):将指定的元素插入此列表的前面。
  8. boolean offerLast(E e): Inserts the specified element at the end of this list.boolean offerLast(E e):在此列表的末尾插入指定的元素。
  9. E pollFirst(): Retrieves and removes the first element of this list, or returns null if this list is empty.E pollFirst():检索并删除此列表的第一个元素,如果此列表为空,则返回null。
  10. E pollLast(): Retrieves and removes the last element of this list, or returns null if this list is empty.E pollLast():检索并删除此列表的最后一个元素;如果此列表为空,则返回null。
  11. E peekFirst(): Retrieves, but does not remove, the first element of this list, or returns null if this list is empty.E peekFirst():检索但不删除此列表的第一个元素,如果此列表为空,则返回null。
  12. E peekLast(): Retrieves, but does not remove, the last element of this list, or returns null if this list is empty.E peekLast():检索但不删除此列表的最后一个元素,如果此列表为空,则返回null。

Java LinkedList基本示例 (Java LinkedList Basic Example)

In this section, we will discuss about Java LinkedList basic example. We will explore some more useful operations in the coming sections.

在本节中,我们将讨论有关Java LinkedList的基本示例。 我们将在接下来的部分中探索一些更有用的操作。

Example:-

例:-

import java.util.LinkedList;
import java.util.List;public class LinkedListDemo
{public static void main(String[] args) {List names = new LinkedList();names.add("Rams");names.add("Posa");names.add("Chinni");names.add(2011);System.out.println("LinkedList content: " + names);System.out.println("LinkedList size: " + names.size());}
}

Output:-

输出:-

LinkedList content: [Rams, Posa, Chinni, 2011]
LinkedList size: 4

Here we have created a LinkedList object and added 4 items. As we discussed LinkedList.size() method is used to get the number of elements in the list.

在这里,我们创建了一个LinkedList对象,并添加了4个项目。 正如我们所讨论的,LinkedList.size()方法用于获取列表中的元素数。

NOTE:- Without using Generics, Java LinkedList supports Heterogeneous elements. However, it is not recommended to use Collections without Generics. Let us explore Java Generics Advantages and usage in the coming section with one simple example.

注意:-在不使用泛型的情况下,Java LinkedList支持异构元素。 但是,不建议使用没有泛型的集合。 让我们通过一个简单的示例在下一部分中探讨Java泛型的优点和用法。

Java LinkedList泛型 (Java LinkedList Generics)

In this section, we will discuss on how to use Generics with Java LinkedList. As we know, Java Generics are useful to write Type Safety programming and do Stronger type checks at compile time. They are also useful to eliminate the casting overhead.

在本节中,我们将讨论如何在Java LinkedList中使用泛型。 众所周知,Java泛型对于编写类型安全编程和在编译时进行更强的类型检查很有用。 它们对于消除铸造开销也很有用。

Example:-

例:-

import java.util.LinkedList;
import java.util.List;public class LinkedListGenericsDemo
{public static void main(String[] args) {List<String> names = new LinkedList<>();names.add("Rams");names.add("Posa");names.add("Chinni");// We cannot add other than Strings// names.add(2011);System.out.println("LinkedList content: " + names);System.out.println("LinkedList size: " + names.size());}
}

Output:-

输出:-

LinkedList content: [Rams, Posa, Chinni]
LinkedList size: 3

Here we have created a LinkedList object with Generics and added 3 items. When we try to add a Number for LinkedList, it throws compile-time error.

在这里,我们使用泛型创建了一个LinkedList对象,并添加了3个项目。 当我们尝试为LinkedList添加一个Number时,它将引发编译时错误。

Java数组到LinkedList (Java Array to LinkedList)

In this section, we will explore how to convert a Java Array to LinkedList object. We can do it in many ways, however I have given only one approach here.

在本节中,我们将探讨如何将Java数组转换为LinkedList对象。 我们可以通过多种方式来做到这一点,但是我在这里只给出了一种方法。

Example:-

例:-

import java.util.LinkedList;
import java.util.List;public class JavaArrayToLinkedListDemo
{public static void main(String[] args) {Integer[] numbers = {1,2,3,4,5};List<Integer> numbersList = new LinkedList<>();for(Integer s : numbers){numbersList.add(s);}System.out.println(numbersList);}
}

Output:-

输出:-

[1, 2, 3, 4, 5]

Java LinkedList到数组 (Java LinkedList to Array)

In this section, we will explore how to convert a Java LinkedList to an Array. We can do it in many ways, however I have given only one approach here.

在本节中,我们将探讨如何将Java LinkedList转换为数组。 我们可以通过多种方式来做到这一点,但是我在这里只给出了一种方法。

Example:-

例:-

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;public class LinkedListToJavaArrayDemo
{public static void main(String[] args) {       List<Integer> numbersList = new LinkedList<>();numbersList.add(1);numbersList.add(2);numbersList.add(3);numbersList.add(4);numbersList.add(5);Integer[] numbers = new Integer[numbersList.size()];numbers = numbersList.toArray(numbers);System.out.println(Arrays.toString(numbers));}
}

Output:-

输出:-

[1, 2, 3, 4, 5]

Java LinkedList实时用例 (Java LinkedList Real-time Usecases)

In this section, we will discuss about what is the best and what is the worst case scenarios to use LinkedList in Java applications.

在本节中,我们将讨论在Java应用程序中使用LinkedList的最佳情况和最坏情况。

Best Usecase scenario:-

最佳用例方案:-

  • When our frequently used operation is adding or removing elements in the middle of the List, LinkedList is the best class to use.当我们经常使用的操作是在List的中间添加或删除元素时,LinkedList是最适合使用的类。

Why? Because we don’t need to do more shifts to add or remove elements at the middle of the list. Please refer “How Insertion works in J Java LinkedList?” section to understand it in-detail.

为什么? 因为我们不需要做更多的改动就可以在列表的中间添加或删除元素。 请参阅“如何在J Java LinkedList中插入?” 部分以详细了解它。

Worst Usecase scenario:-

最坏的用例场景:-

  • When our frequently used operation is retrieving elements from list, then LinkedList is the worst choice.当我们经常使用的操作是从列表中检索元素时,LinkedList是最糟糕的选择。

Why? Because LinkedList supports only sequential access, does NOT support random access. Please refer “How Deletion works in J Java LinkedList?” section to understand it in-detail.

为什么? 由于LinkedList仅支持顺序访问,因此不支持随机访问。 请参阅“删除如何在J Java LinkedList中工作?” 部分以详细了解它。

NOTE:-
LinkedList implements List, Deque, Cloneable and Serializable. But it does NOT implement RandomAccess interface.

注意:-
LinkedList实现列表,双端队列,可克隆和可序列化。 但是它没有实现RandomAccess接口。

Java LinkedList的内部表示 (Internal Representation of Java LinkedList)

As we know, internally Java LinkedList is implemented using Doubly Linked List. So Java LinkedList represents it’s elements as Nodes. Each Node is divided into 3 portions as shown below.

众所周知,内部Java LinkedList是使用双链表实现的。 因此,Java LinkedList将其元素表示为Nodes。 每个节点分为3部分,如下所示。

Here each Node is used for a specific purpose.

在此,每个节点都用于特定目的。

  1. Left side Node Part is used to point to the previous Node (Or Element) in the LinkedList.左侧节点部分用于指向LinkedList中的上一个节点(或元素)。
  2. Right side Node Part is used to point to the next Node (Or Element) in the LinkedList.右侧“节点部分”用于指向LinkedList中的下一个节点(或元素)。
  3. Center Node Part is used to store actual data.中心节点部分用于存储实际数据。

NOTE:- In JVM, LinkedList does NOT store it’s elements in consecutive order. It stores it’s elements at any available space and they are connected each other using Left and Right side Node portions as shown in the below diagram.

注意:-在JVM中,LinkedList不会按连续顺序存储其元素。 它将元素存储在任何可用空间中,并使用左侧和右侧Node部分将它们相互连接,如下图所示。

Java LinkedList中插入如何工作? (How Insertion works in Java LinkedList?)

We have already seen how LinkedList stores it’s elements as Nodes in the previous section. In this section, we will discuss about how Java LinkedList’s Insertion operation works internally.

在上一节中,我们已经看到LinkedList如何将其元素存储为Nodes。 在本节中,我们将讨论Java LinkedList的插入操作如何在内部工作。

  1. Let us assume our initial LinkedList has the following data.让我们假设我们的初始LinkedList具有以下数据。
  2. Perform the following Insertion operation on this LinkedList在此LinkedList上执行以下插入操作
  3. linkedList.add(2,54);

    Here we are trying to perform Insertion operation to add new element with value “54” at index 2.

    在这里,我们尝试执行插入操作以在索引2处添加值为“ 54”的新元素。

  4. Updated LinkedList looks like below.更新的LinkedList如下所示。

删除如何在Java LinkedList中工作? (How Deletion works in Java LinkedList?)

We have already seen how LinkedList performs Insertion operation internally in the previous section. In this section, we will discuss about how Java LinkedList’s Deletion operation works internally.

在上一节中,我们已经看到LinkedList如何在内部执行插入操作。 在本节中,我们将讨论Java LinkedList的Deletion操作如何在内部工作。

  1. Let us assume our initial LinkedList has the following data.让我们假设我们的初始LinkedList具有以下数据。
  2. Perform the following Insertion operation on this LinkedList在此LinkedList上执行以下插入操作
  3. linkedList.remove(3);

    Here we are trying to perform Deletion operation to delete an element which is available at index 3.

    在这里,我们尝试执行删除操作以删除索引3处可用的元素。

  4. Updated LinkedList looks like below.更新的LinkedList如下所示。

Java LinkedList双端队列操作 (Java LinkedList Deque Operations)

Here we will explore how a LinkedList object works as a Deque. We use these operations to implement Queues or Stacks. We will discuss how a Stack or Queues works in-depth in my coming posts.

在这里,我们将探讨LinkedList对象如何作为双端队列。 我们使用这些操作来实现队列或堆栈。 我们将在我的后续文章中深入讨论堆栈或队列的工作方式。

Example:-

例:-

import java.util.LinkedList;
import java.util.LinkedList;
import java.util.Deque;public class LinkedListDequeOperationsDemo
{public static void main(String[] args) {Deque names = new LinkedList();names.add(2);names.addFirst(1);names.addLast(3);names.addFirst(0);names.addLast(4);System.out.println("LinkedList content: " + names);System.out.println("LinkedList size: " + names.size());names.removeFirst();names.removeLast();System.out.println("LinkedList content: " + names);System.out.println("LinkedList size: " + names.size()); }
}

Output:-

输出:-

LinkedList content: [0, 1, 2, 3, 4]
LinkedList size: 5
LinkedList content: [1, 2, 3]
LinkedList size: 3

Java SE 8:要发送的Java LinkedList (Java SE 8: Java LinkedList to Stream)

Here we will explore how to convert a LinkedList object to Java SE 8 Stream concept.

在这里,我们将探讨如何将LinkedList对象转换为Java SE 8 Stream概念。

Example:-

例:-

import java.util.LinkedList;
import java.util.List;public class LinkedListToStreamDemo
{public static void main(String[] args) {       List<Integer> numbersList = new LinkedList<>();numbersList.add(1);numbersList.add(2);numbersList.add(3);numbersList.add(4);numbersList.add(5);//convert List to streamnumbersList.stream().forEach(System.out::println);}
}

Output:-

输出:-

1
2
3
4
5

Java SE 9链表 (Java SE 9 LinkedList)

In Java SE 9, Oracle Corp is going to add some useful utility methods to create Immutable List. If you want to learn them in-depth with some useful examples, please go through my post at: Java SE 9: Factory Methods for Immutable List

在Java SE 9中,Oracle Corp将添加一些有用的实用程序方法来创建不可变列表。 如果您想通过一些有用的示例来深入学习它们,请阅读以下文章: Java SE 9:不可变列表的工厂方法

That’s all of a quick roundup on LinkedList in Java. I hope these Java LinkedList examples will help you in getting started with LinkedList programming.

这就是Java中LinkedList的快速总结。 我希望这些Java LinkedList示例可以帮助您入门LinkedList编程。

Thank you for reading my tutorials. Please drop me a comment if you like my tutorials or have any issues or suggestions or any type errors.

感谢您阅读我的教程。 如果您喜欢我的教程或有任何问题或建议或任何类型错误,请给我评论。

翻译自: https://www.journaldev.com/13386/java-linkedlist-linkedlist-java

Java LinkedList – Java中的LinkedList相关推荐

  1. Java中的LinkedList

    介绍: LinkedList是由节点组成的线性数据结构. 在单链列表中,每个节点包含数据和引用. 在此,参考部分引用链表中的下一个节点. 另一方面,在双链表中,我们具有指向上一个和下一个节点的数据和引 ...

  2. java链表.set_Java中的数组、链表、List、ArrayList、LinkedList、Vector、Set

    数组:数组是在内存中存储相同数据类型的连续的空间  -----声明一个数组就是在内存空间中划出一串连续的空间 1.数组名代表的是连续空间的首地址 2.通过首地址可以依次访问数组所有元素 3.元素在数组 ...

  3. Java -- 基于JDK1.8的LinkedList源码分析

    1,上周末我们一起分析了ArrayList的源码并进行了一些总结,因为最近在看Collection这一块的东西,下面的图也是大致的总结了Collection里面重要的接口和类,如果没有意外的话后面基本 ...

  4. 链表用java实现简单单链表linkedlist

    链表是一种物理存储单元上非连续.非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的. 每个结点包括两个部分: 1是存储数据元素的数据域data 2是存储下一个结点地址的指针域p. 相 ...

  5. JAVA复习5(集合——LinkedList)

    LinkedList 这个子类是基于链表的实现   指针 -节点-->-> 以下为LinkedList类的定义 public class LinkedList<E> exten ...

  6. java 双向链表_java集合类之LinkedList

    LinkedList简介 LinkedList 是一个继承于AbstractSequentialList的双向链表.它也可以被当作堆栈.队列或双端队列进行操作. LinkedList 实现 List ...

  7. Java用户定义对象的LinkedList

    Java用户定义对象的LinkedList 要在Java中对列表进行排序,可以使用sort(List<T> list) 方法.此方法可以对列表进行排序,其中所有元素都必须实现Compara ...

  8. java 程序语言中的LinkedList 集合基本方法演示

    java 程序语言中的LinkedList 集合基本方法演示 import java.util.Iterator; import java.util.LinkedList;public class L ...

  9. java linkedlist 节点_JAVA学习-LinkedList详解

    1.定义 实现List接口与Deque接口双向链表,实现了列表的所有操作,并且允许包括null值的所有元素,对于LinkedList定义我产生了如下疑问: 1.Deque接口是什么,定义了一个怎样的规 ...

最新文章

  1. 193. 一个不错的日历插件
  2. Javaweb经典三层架构的演变
  3. python入门心得_一天入门 Python 的一些心得
  4. 使用RMAN对数据文件进行恢复
  5. 八款Android开源游戏引擎
  6. Win10下安装CentOS双系统详解
  7. 计算机主板pci插槽,什么是PCI插槽 PCI-E插槽如何辨别【详解】
  8. javascript飞机大战
  9. UT000010: Session is invalid
  10. 水仙花数python代码多种方式_水仙花数的三种解决方法(Python实现)
  11. mx250是什么_MX250和150有什么区别 MX250和MX150区别对比介绍
  12. 阅读笔记 |《上帝掷骰子吗:量子物理史话》曹天元
  13. 《三体》刘慈欣:意识上传离现实还存在很大技术障碍
  14. Win 7或者 Win 8.1系统下安装centOS 7双系统方法(亲测)
  15. CCNA-NAT协议 静态NAT 动态NAT(多对多) PAT(多对一) 内网与外网之间的通信 GNS3实验验证
  16. 脑注意力机制启发的群体智能协同避障方法
  17. 淘宝运营是什么意思?运营要学习的内容有哪些?
  18. HDM和IDVI都是数字显示器的接口
  19. 黄金斗士原生android,联想黄金斗士S8 (S898T+)官方固件rom系统刷机包
  20. gtx1660是什么级别的_GTX1660配什么CPU和主板 GTX1660显卡搭配指南

热门文章

  1. gvim的常用编辑快捷键
  2. [转载] Python 字典删除元素clear、pop、popitem
  3. [转载] Python dict.keys方法
  4. [转载] python flask实现分页
  5. MATLAB IIR滤波器设计函数buttord与butter
  6. python里我最容易搞不清楚问题之一的encode和decode
  7. SignalR 跨域解决方案全面
  8. [USACO5.1] Musical Themes
  9. python---之os.path.splitext(“文件路径”)
  10. 数据结构上机实践第四周项目1 - 建立单链表