📜  Java.util.LinkedList.peek()、peekfirst()、peeklast() 在Java中

📅  最后修改于: 2022-05-13 01:55:20.489000             🧑  作者: Mango

Java.util.LinkedList.peek()、peekfirst()、peeklast() 在Java中

链表类提供“查看”列表的第一个和最后一个元素的功能,因此在只需要检索而不一定需要删除的情况下很有用。存在三个功能,所有功能都在本文中进行了讨论。 1. peek() :此方法检索但不删除此列表的头部(第一个元素)。

Declaration : 
   public E peek()
Return Value : 
  This method returns the head of this list, or null if this list is empty.
// Java code to demonstrate the working
// of peek() in LinkedList
import java.util.*;
public class LinkedPeek1 {
  
public static void main(String[] args)
    {
        // declaring a LinkedList
        LinkedList list = new LinkedList();
  
        // adding  elements
        list.add("Geeks");
        list.add(4);
        list.add("Geeks");
        list.add("8");
  
        // printing the list
        System.out.println("The initial list is :" + list);
  
        // peek at the head of the list
        // Prints 1st element, "Geeks"
        System.out.println("Head of the list : " + list.peek());
    }
}

输出:

The initial list is :[Geeks, 4, Geeks, 8]
Head of the list : Geeks

2. peekFirst() :此方法检索但不删除此列表的第一个元素,如果此列表为空,则返回 null。这与 peek() 类似。

Declaration : 
   public E peekFirst()
Return Value : 
    This method returns the first element of this list, or null if this list is empty
// Java code to demonstrate the working
// of peekFirst() in LinkedList
import java.util.*;
public class LinkedPeek2 {
  
public static void main(String[] args)
    {
        // declaring a LinkedList
        LinkedList list = new LinkedList();
  
        // adding  elements
        list.add("Geeks");
        list.add(4);
        list.add("Geeks");
        list.add("8");
  
        // printing the list
        System.out.println("The initial list is :" + list);
  
        // peek at the first element of the list
        // Prints 1st element, "Geeks"
        System.out.println("First element of the list is : " + list.peekFirst());
    }
}

输出:

The initial list is :[Geeks, 4, Geeks, 8]
First element of the list is : Geeks

3. peekLast() :此方法检索但不删除此列表的最后一个元素,如果此列表为空,则返回 null。

Declaration
  public E peekLast()
Return Value
   This method returns the last element of this list, or null if this list is empty
// Java code to demonstrate the working
// of peekLast() in LinkedList
import java.util.*;
public class LinkedPeek3 {
  
public static void main(String[] args)
    {
        // declaring a LinkedList
        LinkedList list = new LinkedList();
  
        // adding  elements
        list.add("Geeks");
        list.add(4);
        list.add("Geeks");
        list.add(8);
  
        // printing the list
        System.out.println("The initial list is :" + list);
  
        // peek at the last element of the list
        // Prints last element, 8
        System.out.println("Last element of the list is : " + list.peekLast());
    }
}

输出:

The initial list is :[Geeks, 4, Geeks, 8]
Last element of the list is : 8

实际应用:可以想到的实际应用是,这可以潜在地用于纸牌游戏中,个人可以在询问他们想看哪个元素时偷看牌组的第一个或最后一个元素。下面的代码解释了工作。

// Java code to demonstrate the application
// of peek()
import java.util.*;
public class LinkedPeekApp {
  
public static void main(String[] args)
    {
  
        // declaring a LinkedList
        LinkedList list = new LinkedList();
  
        // adding  elements in deck
        list.add(5);
        list.add(4);
        list.add("Jack");
        list.add(8);
        list.add("King");
  
        // printing the list
        System.out.println("The initial deck is :" + list);
  
        String d = "upper";
  
        System.out.println("The element chosen to peek is : " + d);
  
        if (d == "upper")
            System.out.println("The Upper element is : " + list.peekFirst());
        else
            System.out.println("The Lower element is : " + list.peekLast());
    }
}

输出 :

The initial deck is :[5, 4, Jack, 8, King]
The element chosen to peek is : upper
The Upper element is : 5