如何在Java中迭代LinkedList?
LinkedHashMap类类似于HashMap,但与HashMap相比,它具有一个附加功能。LinkedList类属于java.util包。
LinkedList使用双向链表来存储其元素。鉴于我们的操作通常包括插入和删除,LinkedList是最佳选择。
java.util包包含LinkedList集合框架。它作为非连续LinkedList数据结构的实现,在内存中保存元素。
使用的方法
您可以使用五种主要方法来迭代HashMap:
使用for循环
使用while循环
使用增强型for循环或for-each循环
使用迭代器
使用forEach()方法
方法1:使用for循环迭代LinkedList
for循环是迭代Java中LinkedList元素的最简单方法。为了获取列表中元素的数量,我们使用size()方法。然后将使用for循环迭代这些元素。
size()
这段代码创建了一个名为“list”的LinkedList对象,并向其中添加了四个元素:“Java”、“c语言”、“Python”和“Ruby”。然后它使用for循环迭代链表并打印每个元素。
算法
步骤1 - 从java.util包导入必要的LinkedList类。
步骤2 - 在iterateLinkedList包中创建一个名为LinkedListEx1的公共类。
步骤3 - 定义main方法。
步骤4 - 声明并初始化一个名为list的空LinkedList对象来存储字符串。
步骤5 - 使用add方法向列表添加四个元素(“Java”、“C语言”、“Python”和“Ruby”)。
步骤6 - 开始遍历链表的元素。您可以使用for循环来完成。
步骤7 - 在每次迭代中,使用get方法检索当前索引处的元素。然后将其保存在element变量中。
步骤8 - 打印element变量的值,该变量表示列表中的每个字符串。
示例
package iterateLinkedList;
import java.util.LinkedList;
public class LinkedListEx1 {
public static void main(String[] args) {
// Generate a generic LinkedList object of string type.
LinkedList<String> list = new LinkedList<String>(); // an empty list.
// Adding elements in the list.
list.add("Java");
list.add("C Language");
list.add("Python");
list.add("Ruby");
// Iterating linked list using for-each loop.
System.out.println("For-each loop Method");
for (String element : list) {
System.out.println(element);
}
}
}
输出
For-each loop Method Java C Language Python Ruby
方法2:使用while循环迭代
while循环是迭代链表条目的第二种方法。它对于LinkedList中少量数据有效。
get()
此代码示例生成一个名为“cities”的LinkedList。然后向其中添加四个字符串。然后它使用while循环迭代链表并打印每个元素。
算法
步骤1 - 创建一个名为“cities”的空LinkedList。
步骤2 - 将字符串“Bangalore”、“Mumbai”、“Kolkatta”和“Chennai”添加到cities列表。
步骤3 - 指定一个计数器变量“num”,其默认值为0。
步骤4 - 进入一个while循环,只要cities列表的大小大于“num”的值,该循环就继续。
步骤5 - 在每次迭代中,使用get方法检索当前索引(由“num”确定)处的元素并打印它。
步骤6 - 将“num”的值增加1。
步骤7 - 结束循环和程序。
示例
package iterateLinkedList;
import java.util.LinkedList;
public class Tutorialspoint {
public static void main(String[] args) {
// generate a LinkedList object of string type.
LinkedList<String> cities = new LinkedList<String>();
// Addition of elements of only string type.
cities.add("Bangalore");
cities.add("Mumbai");
cities.add("Kolkatta");
cities.add("Chennai");
System.out.println("**Using While Loop for iteration of LinkedList **");
int num = 0;
while (cities.size() > num) {
System.out.println(cities.get(num));
num++;
}
}
}
输出
**Using While Loop for iteration of LinkedList ** Bangalore Mumbai Kolkatta Chennai
方法3:使用增强型for循环迭代
使用for-each循环或增强型for循环迭代Java中的LinkedList是另一种方法。在此函数中,我们使用增强型for循环直接迭代LinkedList的元素。
for()
这段代码创建一个LinkedList并向其中添加三个字符串。然后它使用增强型for循环迭代LinkedList的元素并打印它们。
算法
步骤1 - 从java.util包导入所需的LinkedList类。
步骤2 - 创建公共类Tutorialspoint。
步骤3 - 创建main方法。
步骤4 - 创建一个名为linkedList的LinkedList对象来存储字符串。
步骤5 - 使用add方法将字符串“Welcome”、“to”和“Tutorialspoint”添加到链表。
步骤6 - 调用iterateUsingEnhancedForLoop方法并将linkedList作为参数传递。
步骤7 - 定义iterateUsingEnhancedForLoop方法,该方法接受字符串的LinkedList作为参数。
步骤8 - 打印初始消息“使用增强型for循环迭代LinkedList:”。
步骤9 - 使用增强型for循环迭代linkedList中的每个元素(listElement)。
步骤10 - 在每次迭代中,打印listElement的值,后跟一个空格。
步骤11 - 关闭循环和程序。
示例
import java.util.LinkedList;
public class Tutorialspoint {
public static void main(String[] args){
LinkedList<String> linkedList = new LinkedList<>();
linkedList.add("Welcome");
linkedList.add("to");
linkedList.add("Tutorialspoint");
iterateUsingEnhancedForLoop(linkedList);
}
public static void iterateUsingEnhancedForLoop(LinkedList<String> linkedList){
System.out.print("Iterating the LinkedList with the help of enhanced for loop : ");
for (String listElement : linkedList) {
System.out.print(listElement + " ");
}
}
}
输出
Iterating the LinkedList with the help of enhanced for loop : Welcome to Tutorialspoint
方法4:使用迭代器
使用迭代器是最好的方法之一。它只不过是使用iterable接口提供的iterator()方法来创建一个Iterator对象。然后我们可以使用此Iterator对象来迭代LinkedList的元素。
while()
此代码示例创建一个LinkedList并向其中添加五个字符。然后它使用Iterator迭代LinkedList并打印每个字符。
算法
步骤1 - 从java.util包导入必要的Iterator和LinkedList类。
步骤2 - 定义一个名为Tutorialspoint的公共类。
步骤3 - 定义main方法。
步骤4 - 创建一个名为list的LinkedList对象来存储字符。
步骤5 - 使用add方法将字符'V'、'W'、'X'、'Y'和'Z'添加到LinkedList。
步骤6 - 打印初始消息“使用迭代器进行迭代”。
步骤7 - 使用list的iterator方法创建一个Iterator对象itr。
步骤8 - 进入一个while循环,只要LinkedList中还有更多元素(itr.hasNext()),该循环就继续。
步骤9 - 在每次迭代中,从Iterator(itr.next())检索下一个元素并将其存储在Character类型的obj变量中。
步骤10 - 打印obj的值。
示例
package iterateLinkedList;
import java.util.Iterator;
import java.util.LinkedList;
public class Tutorialspoint {
public static void main(String[] args) {
LinkedList<Character> list = new LinkedList<>();
list.add('V');
list.add('W');
list.add('X');
list.add('Y');
list.add('Z');
System.out.println("Using Iterator for iteration");
Iterator<Character> itr = list.iterator();
while (itr.hasNext()) {
Character obj = itr.next();
System.out.println(obj);
}
}
}
输出
Using Iterator for iteration V W X Y Z
方法5:使用forEach()
Java 8标志着forEach()方法的首次亮相。直到所有项目都已处理或过程遇到错误,每个Iterable元素都将受到任务执行的约束。
forEach()
这段代码创建一个LinkedList并向其中添加五个整数。然后使用forEach方法迭代LinkedList的元素,并显示它们。
算法
步骤1 - 从java.util包导入必要的LinkedList类。
步骤2 - 定义一个名为TLP的公共类。
步骤3 - 定义main方法。
步骤4 - 创建一个名为linkedList的LinkedList对象来存储整数。
步骤5 - 使用add方法将整数100、200、300、400和500添加到LinkedList。
步骤6 - 调用iterateUsingForEach方法并将linkedList作为参数传递。
步骤7 - 定义iterateUsingForEach方法,该方法接受整数的LinkedList作为参数。
步骤8 - 打印初始消息“使用forEach函数迭代LinkedList:”。
步骤9 - 使用LinkedList的forEach函数,传递一个lambda表达式,该表达式打印每个元素,后跟一个空格。
步骤10 - 终止程序
示例
import java.util.LinkedList;
public class TLP {
public static void main(String[] args){
LinkedList<Integer> linkedList = new LinkedList<>();
linkedList.add(100);
linkedList.add(200);
linkedList.add(300);
linkedList.add(400);
linkedList.add(500);
iterateUsingForEach(linkedList);
}
public static void iterateUsingForEach(LinkedList<Integer> linkedList){
System.out.print("Iteration of the LinkedList by employing for each function : ");
linkedList.forEach((element) -> System.out.print(element + " "));
}
}
输出
Iteration of the LinkedList by employing for each function : 100 200 300 400 500
结论
LinkedHashMap是HashMap的一个特殊子类,它包含一个双向链表,该链表连接其所有条目。与HashMap不同,LinkedHashMap保留元素插入的顺序。此特性使LinkedHashMap成为需要按其原始插入顺序遍历映射条目的应用程序的理想选择。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP