- Java.util 包类
- Java.util - 首页
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util 包额外内容
- Java.util - 接口
- Java.util - 异常
- Java.util - 枚举
- Java.util 有用资源
- Java.util - 有用资源
- Java.util - 讨论
Java LinkedList peek() 方法
描述
Java LinkedList peek() 方法检索此 linkedList 表示的列表的头部(但不删除)。如果此 linkedList 为空,则返回 null。此方法类似于 getFirst() 方法。
声明
以下是 java.util.LinkedList.peek() 方法的声明
public E peek()
参数
无
返回值
此方法返回此 linkedList 表示的列表的头部,如果此 linkedList 为空,则返回 null。
异常
无
获取整数 LinkedList 的第一个元素示例
以下示例演示了 Java LinkedList peek() 方法与整数一起使用的用法。我们正在创建一个整数 LinkedList,添加一些元素,打印它,然后使用 peek() 方法获取第一个元素。LinkedList 保持不变。
package com.tutorialspoint; import java.util.LinkedList; public class LinkedListDemo { public static void main(String[] args) { // create an empty linkedList LinkedList<Integer> linkedList = new LinkedList<>(); // use add() method to add elements in the linkedList linkedList.add(25); linkedList.add(30); linkedList.add(20); linkedList.add(18); // let us print all the elements available in linkedList System.out.println("LinkedList = " + linkedList); // it will retrieve first element System.out.println("Retrieved Element is = " + linkedList.peek()); // let us print all the elements available in linkedList again System.out.println("LinkedList = " + linkedList); } }
输出
让我们编译并运行上述程序,这将产生以下结果:
LinkedList = [25, 30, 20, 18] Retrieved Element is = 25 LinkedList = [25, 30, 20, 18]
获取字符串 LinkedList 的第一个元素示例
以下示例演示了 Java LinkedList peek() 方法与字符串一起使用的用法。我们正在创建一个字符串 LinkedList,添加一些元素,打印它,然后使用 peek() 方法获取第一个元素。LinkedList 保持不变。
package com.tutorialspoint; import java.util.LinkedList; public class LinkedListDemo { public static void main(String[] args) { // create an empty linkedList LinkedList<String> linkedList = new LinkedList<>(); // use add() method to add elements in the linkedList linkedList.add("A"); linkedList.add("B"); linkedList.add("C"); linkedList.add("D"); // let us print all the elements available in linkedList System.out.println("LinkedList = " + linkedList); // it will retrieve first element System.out.println("Retrieved Element is = " + linkedList.peek()); // let us print all the elements available in linkedList again System.out.println("LinkedList = " + linkedList); } }
输出
让我们编译并运行上述程序,这将产生以下结果:
LinkedList = [A, B, C, D] Retrieved Element is = A LinkedList = [A, B, C, D]
获取对象 LinkedList 的第一个元素示例
以下示例演示了 Java LinkedList peek() 方法与 Student 对象一起使用的用法。我们正在创建一个 Student 对象的 LinkedList,添加一些学生,打印它,然后使用 peek() 方法获取第一个学生。LinkedList 保持不变。
package com.tutorialspoint; import java.util.LinkedList; public class LinkedListDemo { public static void main(String[] args) { // create an empty linkedList LinkedList<Student> linkedList = new LinkedList<>(); // use add() method to add elements in the linkedList linkedList.add(new Student(1, "Julie")); linkedList.add(new Student(2, "Robert")); linkedList.add(new Student(3, "Adam")); // let us print all the elements available in linkedList System.out.println("LinkedList = " + linkedList); // it will retrieve first element System.out.println("Retrieved Element is = " + linkedList.peek()); // let us print all the elements available in linkedList again System.out.println("LinkedList = " + linkedList); } } class Student { int rollNo; String name; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + " ]"; } @Override public boolean equals(Object obj) { Student s = (Student)obj; return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name); } }
输出
让我们编译并运行上述程序,这将产生以下结果:
LinkedList = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]] Retrieved Element is = [ 1, Julie ] LinkedList = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
java_util_linkedlist.htm
广告