Método LinkedBlockingQueue peek() en Java

El método peek() de LinkedBlockingQueue devuelve el encabezado de LinkedBlockingQueue. Recupera el valor del encabezado de LinkedBlockingQueue pero no lo elimina. Si LinkedBlockingQueue está vacío, este método devuelve un valor nulo.

Sintaxis:

public E peek()

Valor devuelto: este método devuelve el encabezado de LinkedBlockingQueue.

Los siguientes programas ilustran el método peek() de la clase LinkedBlockingQueue:

Programa 1: Devolver el encabezado de LinkedBlockingQueue usando el método peek() donde LinkedBlockingQueue de capacidad 7 contiene una lista de nombres

// Java Program Demonstrate peek()
// method of LinkedBlockingQueue
  
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
  
    public static void main(String[] args)
    {
        // define capacity of LinkedBlockingQueue
        int capacityOfQueue = 7;
  
        // create object of LinkedBlockingQueue
        LinkedBlockingQueue<String> linkedQueue
            = new LinkedBlockingQueue<String>(capacityOfQueue);
  
        // Add element to LinkedBlockingQueue
        linkedQueue.add("John");
        linkedQueue.add("Tom");
        linkedQueue.add("Clark");
        linkedQueue.add("Kat");
  
        // find head of linkedQueue using peek() method
        String head = linkedQueue.peek();
  
        // print result
        System.out.println("Queue is " + linkedQueue);
  
        // print head of queue
        System.out.println("Head of Queue is " + head);
  
        // removing one element
        linkedQueue.remove();
  
        // again get head of queue
        head = linkedQueue.peek();
  
        // print result
        System.out.println("\nRemoving one element from Queue\n");
        System.out.println("Queue is " + linkedQueue);
  
        // print head of queue
        System.out.println("Head of Queue is " + head);
    }
}
Producción:

Queue is [John, Tom, Clark, Kat]
Head of Queue is John

Removing one element from Queue

Queue is [Tom, Clark, Kat]
Head of Queue is Tom

Programa 2: encontrar el encabezado de LinkedBlockingQueue que contiene una lista de empleados

// Java Program Demonstrate peek()
// method of LinkedBlockingQueue
  
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
  
    public void findPeek()
    {
        // define capacity of LinkedBlockingQueue
        int capacityOfQueue = 7;
  
        // create object of LinkedBlockingQueue
        LinkedBlockingQueue<Employee> linkedQueue
            = new LinkedBlockingQueue<Employee>(capacityOfQueue);
  
        // Add element to LinkedBlockingQueue
        Employee emp1 = new Employee("Ravi", "Tester", "39000");
        Employee emp2 = new Employee("Sanjeet", "Manager", "98000");
  
        // Add Employee Objects to linkedQueue
        linkedQueue.add(emp1);
        linkedQueue.add(emp2);
  
        // print head then remove element
        // and follow same process again
        // until the queue becomes empty
  
        while (linkedQueue.size() != 0) {
  
            // find head of linkedQueue using peek() method
            Employee head = linkedQueue.peek();
  
            // print results
            System.out.println("Head of list");
            System.out.println("Employee Name : " + head.name);
            System.out.println("Employee Position : " + head.position);
            System.out.println("Employee Salary : " + head.salary);
  
            linkedQueue.remove();
            if (linkedQueue.size() != 0)
                System.out.println("\nRemoving one element from Queue\n");
        }
    }
  
    // create an Employee Object with
    // position and salary as an attribute
    public class Employee {
  
        public String name;
        public String position;
        public String salary;
        Employee(String name, String position, String salary)
        {
            this.name = name;
            this.position = position;
            this.salary = salary;
        }
        @Override
        public String toString()
        {
            return "Employee [name=" + name + ", position="
                + position + ", salary=" + salary + "]";
        }
    }
    // Main Method
    public static void main(String[] args)
    {
        GFG gfg = new GFG();
        gfg.findPeek();
    }
}
Producción:

Head of list
Employee Name : Ravi
Employee Position : Tester
Employee Salary : 39000

Removing one element from Queue

Head of list
Employee Name : Sanjeet
Employee Position : Manager
Employee Salary : 98000

Referencia: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingQueue.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

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *