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
广告