El método addAll() de ConcurrentLinkedQueue se utiliza para insertar todos los elementos de la Colección, pasados como parámetro a este método, al final de una ConcurrentLinkedQueue. La inserción del elemento está en el mismo orden en que lo devuelve el iterador de colecciones.
Sintaxis:
public boolean addAll(Collection<? extends E> c)
Parámetro: este método toma un parámetro c que representa la colección cuyos elementos se necesitan agregar al final de esta ConcurrentLinkedQueue.
Devoluciones: este método devuelve verdadero si se realiza al menos una acción de inserción.
Excepción: este método arroja dos excepciones diferentes:
- NullPointerException : si la colección pasada o cualquiera de sus elementos son nulos.
- IllegalArgumentException : si la colección pasada es esta misma cola.
Los siguientes programas ilustran el método addAll() de ConcurrentLinkedQueue:
Ejemplo 1: intentar agregar una lista de números a ConcurrentLinkedQueue.
// Java Program Demonstrate addAll() // method of ConcurrentLinkedQueue import java.util.concurrent.*; import java.util.*; 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); // create a ArrayList of Numbers ArrayList<Integer> arraylist = new ArrayList<Integer>(); // add some numbers to ArrayList arraylist.add(377139); arraylist.add(624378); arraylist.add(654793); arraylist.add(764764); arraylist.add(838494); arraylist.add(632845); // Displaying the existing ConcurrentLinkedQueue System.out.println("ConcurrentLinkedQueue: " + queue); // Displaying the existing Collection System.out.println("Collection to be added: " + arraylist); // apply addAll() method and passed the arraylist as parameter boolean response = queue.addAll(arraylist); // Displaying the existing ConcurrentLinkedQueue System.out.println("Collection added: " + response); // Displaying the existing ConcurrentLinkedQueue System.out.println("ConcurrentLinkedQueue: " + queue); } }
ConcurrentLinkedQueue: [4353] Collection to be added: [377139, 624378, 654793, 764764, 838494, 632845] Collection added: true ConcurrentLinkedQueue: [4353, 377139, 624378, 654793, 764764, 838494, 632845]
Ejemplo 2: intentar agregar una lista de strings a ConcurrentLinkedQueue.
// Java Program Demonstrate addAll() // method of ConcurrentLinkedQueue import java.util.concurrent.*; import java.util.*; 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"); // create a ArrayList of Numbers ArrayList<String> arraylist = new ArrayList<String>(); // add some numbers to ArrayList arraylist.add("Sanjeet"); arraylist.add("Rabi"); arraylist.add("Debasis"); arraylist.add("Raunak"); arraylist.add("Mahesh"); // Displaying the existing ConcurrentLinkedQueue System.out.println("ConcurrentLinkedQueue: " + queue); // Displaying the existing Collection System.out.println("Collection to be added: " + arraylist); // apply addAll() method and passed the arraylist as parameter boolean response = queue.addAll(arraylist); // Displaying the existing ConcurrentLinkedQueue System.out.println("Collection added: " + response); // Displaying the existing ConcurrentLinkedQueue System.out.println("ConcurrentLinkedQueue: " + queue); } }
ConcurrentLinkedQueue: [Aman, Amar] Collection to be added: [Sanjeet, Rabi, Debasis, Raunak, Mahesh] Collection added: true ConcurrentLinkedQueue: [Aman, Amar, Sanjeet, Rabi, Debasis, Raunak, Mahesh]
Ejemplo 3: Mostrar NullPointerException
// Java Program Demonstrate addAll() // method of ConcurrentLinkedQueue import java.util.concurrent.*; import java.util.*; 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"); // create a ArrayList which is equal to null ArrayList<String> arraylist = null; // Displaying the existing ConcurrentLinkedQueue System.out.println("ConcurrentLinkedQueue: " + queue); // Displaying the existing Collection System.out.println("Collection to be added: " + arraylist); try { // apply addAll() method and pass the arraylist as parameter // since the arraylist is null, exception will be thrown boolean response = queue.addAll(arraylist); } catch (NullPointerException e) { System.out.println("Exception thrown while adding null: " + e); } } }
ConcurrentLinkedQueue: [Aman, Amar] Collection to be added: null Exception thrown while adding null: java.lang.NullPointerException
Ejemplo 4: Mostrando IllegalArgumentException
// Java Program Demonstrate addAll() // method of ConcurrentLinkedQueue import java.util.concurrent.*; import java.util.*; 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"); // Displaying the existing ConcurrentLinkedQueue System.out.println("ConcurrentLinkedQueue: " + queue); try { // apply addAll() method and passed the queue as parameter boolean response = queue.addAll(queue); } catch (IllegalArgumentException e) { System.out.println("Exception thrown while adding queue" + " to itself when collection is required: " + e); } } }
ConcurrentLinkedQueue: [Aman, Amar] Exception thrown while adding queue to itself when collection is required: java.lang.IllegalArgumentException
Publicación traducida automáticamente
Artículo escrito por AmanSingh2210 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA