El método toString() de la clase ArrayBlockingQueue se usa para obtener una representación de string de los objetos de ArrayBlockingQueue. La string de ArrayBlockingQueue contiene elementos de ArrayBlockingQueue en el orden del primero (cabeza) al último (cola), entre corchetes («[]»). Los elementos se separan por los caracteres”, ” (coma y un espacio). Básicamente, el método toString() se usa para convertir todos los elementos de ArrayBlockingQueue en una string.
Sintaxis:
public String toString()
Valor de retorno: el método devuelve una representación de string de ArrayBlockingQueue.
Los siguientes programas ilustran el método toString() de la clase ArrayBlockingQueue:
Programa 1:
// Program to demonstrate how to apply toString() method // of ArrayBlockingQueue Class. import java.util.concurrent.ArrayBlockingQueue; public class GFG { public static void main(String[] args) { // Define capacity of ArrayBlockingQueue int capacity = 5; // Create object of ArrayBlockingQueue ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(capacity); // Add 5 elements to ArrayBlockingQueue queue.offer(423); queue.offer(422); queue.offer(421); queue.offer(420); queue.offer(424); // Print queue System.out.println("Queue is "+queue); // Call toString() method and Create an iterator String stringRepresentation=queue.toString(); // Print String value returned by toString() method System.out.println("\nThe String returned by toString():"); System.out.println(stringRepresentation); } }
Queue is [423, 422, 421, 420, 424] The String returned by toString(): [423, 422, 421, 420, 424]
Programa 2:
// Program Demonstrate how to apply toString() method // of ArrayBlockingQueue Class. import java.util.concurrent.ArrayBlockingQueue; public class GFG { public static void main(String[] args) { // Define capacity of ArrayBlockingQueue int capacity = 10; // Create object of ArrayBlockingQueue ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(capacity); // Add 5 elements to ArrayBlockingQueue queue.offer("User"); queue.offer("Employee"); queue.offer("Manager"); queue.offer("Analyst"); queue.offer("HR"); queue.offer("Tester"); // Print queue System.out.println("Queue is "+queue); // Call toString() method and Create an iterator String stringRepresentation=queue.toString(); // Print String value returned by toString() method System.out.println("\nThe String returned by toString():"); System.out.println(stringRepresentation); } }
Queue is [User, Employee, Manager, Analyst, HR, Tester] The String returned by toString(): [User, Employee, Manager, Analyst, HR, Tester]
Referencia: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ArrayBlockingQueue.html#toString
Publicación traducida automáticamente
Artículo escrito por AmanSingh2210 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA