El método peek() de ConcurrentLinkedQueue se usa para devolver el encabezado de ConcurrentLinkedQueue. Recupera, pero no elimina, el encabezado de esta ConcurrentLinkedQueue. Si ConcurrentLinkedQueue está vacío, este método devuelve un valor nulo.
Sintaxis:
public E peek()
Devoluciones: este método devuelve el encabezado de esta ConcurrentLinkedQueue sin eliminarlo.
Los siguientes programas ilustran el método peek() de ConcurrentLinkedQueue:
Ejemplo 1:
// Java Program Demonstrate peek() // method of ConcurrentLinkedQueue import java.util.concurrent.*; public class GFG { public static void main(String[] args) { // create an ConcurrentLinkedQueue ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue<Integer>(); // Add Numbers to queue queue.add(4353); queue.add(7824); queue.add(78249); queue.add(8724); // Displaying the existing ConcurrentLinkedQueue System.out.println("ConcurrentLinkedQueue: " + queue); // find peek int response1 = queue.peek(); // print after applying peek method System.out.println("Head: " + response1); // Verifying that the head is not removed System.out.println("ConcurrentLinkedQueue after peek: " + queue); } }
Producción:
ConcurrentLinkedQueue: [4353, 7824, 78249, 8724] Head: 4353 ConcurrentLinkedQueue after peek: [4353, 7824, 78249, 8724]
Ejemplo 2:
// Java Program Demonstrate peek() // method of ConcurrentLinkedQueue import java.util.concurrent.*; public class GFG { public static void main(String[] args) { // create an ConcurrentLinkedQueue ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>(); // Add String to queue queue.add("Aman"); queue.add("Amar"); queue.add("Sanjeet"); queue.add("Rabi"); // Displaying the existing ConcurrentLinkedQueue System.out.println("ConcurrentLinkedQueue: " + queue); // find peek of queue String response1 = queue.peek(); // print after applying peek method System.out.println("Head: " + response1); // Verifying that the head is not removed System.out.println("ConcurrentLinkedQueue after peek: " + queue); // remove some elements queue.poll(); queue.poll(); // Displaying the existing ConcurrentLinkedQueue System.out.println("Updated ConcurrentLinkedQueue: " + queue); // find peek of queue String response2 = queue.peek(); // print after applying peek method System.out.println("Head: " + response1); // Verifying that the head is not removed System.out.println("ConcurrentLinkedQueue after peek: " + queue); } }
Producción:
ConcurrentLinkedQueue: [Aman, Amar, Sanjeet, Rabi] Head: Aman ConcurrentLinkedQueue after peek: [Aman, Amar, Sanjeet, Rabi] Updated ConcurrentLinkedQueue: [Sanjeet, Rabi] Head: Aman ConcurrentLinkedQueue after peek: [Sanjeet, Rabi]
Referencia: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html#peek–
Publicación traducida automáticamente
Artículo escrito por AmanSingh2210 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA