當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Collection addAll()用法及代碼示例


java.util.Collection接口的addAll(Collection collection)用於將Collection“集合”添加到此現有集合中。此方法返回一個表示操作成功的布爾值。如果添加了集合,則返回true,否則返回false。

用法:

Collection.addAll(Collection<E> collection)

參數:此方法接受要添加到此集合的Collection類型的強製性參數集合。


返回值:此方法返回一個表示操作成功的布爾值。如果添加了集合,則返回true,否則返回false。

異常:此方法引發以下異常:

  • UnsupportedOperationException:如果此集合不支持添加操作
  • ClassCastException:如果指定元素的類阻止將其添加到此集合中
  • NullPointerException :如果指定的元素為null,並且此集合不允許使用null元素
  • IllegalArgumentException:如果元素的某些屬性阻止將其添加到此集合中
  • IllegalStateException:如果由於插入限製當前無法添加該元素

以下示例說明了Collection addAll()方法:

示例1:使用LinkedList類

// Java code to illustrate boolean addAll() 
  
import java.util.*; 
import java.util.*; 
  
public class LinkedListDemo { 
    public static void main(String args[]) 
    { 
  
        // Creating an empty LinkedList 
        Collection<String> 
            list = new LinkedList<String>(); 
  
        // A collection is created 
        Collection<String> 
            collect = new LinkedList<String>(); 
        collect.add("A"); 
        collect.add("Computer"); 
        collect.add("Portal"); 
        collect.add("for"); 
        collect.add("Geeks"); 
  
        // Displaying the list 
        System.out.println("The LinkedList is: " + list); 
  
        // Appending the collection to the list 
        list.addAll(collect); 
  
        // displaying the modified LinkedList 
        System.out.println("The new linked list is: "
                           + list); 
    } 
}
輸出:
The LinkedList is: []
The new linked list is: [A, Computer, Portal, for, Geeks]

示例2:使用ArrayDeque類

// Java code to illustrate addAll() method 
  
import java.util.*; 
  
public class ArrayDequeDemo { 
    public static void main(String args[]) 
    { 
        // Creating an empty ArrayDeque 
        Collection<String> 
            de_que = new ArrayDeque<String>(); 
  
        // Creating a new ArrayDeque 
        Collection<String> 
            deque = new ArrayDeque<String>(); 
        deque.add("Welcome"); 
        deque.add("To"); 
        deque.add("Geeks"); 
        deque.add("4"); 
        deque.add("Geeks"); 
  
        // Displaying the list 
        System.out.println("The ArrayDeque is: " + de_que); 
  
        // Appending the collection to the list 
        de_que.addAll(deque); 
  
        // displaying the modified ArrayDeque 
        System.out.println("The new ArrayDeque is: "
                           + de_que); 
    } 
}
輸出:
The ArrayDeque is: []
The new ArrayDeque is: [Welcome, To, Geeks, 4, Geeks]

示例3:使用ArrayList類

// Java code to illustrate boolean addAll() 
  
import java.util.*; 
  
public class LinkedListDemo { 
    public static void main(String args[]) 
    { 
  
        // Creating an empty ArrayList 
        Collection<String> 
            list = new ArrayList<String>(); 
  
        // A collection is created 
        Collection<String> 
            collect = new ArrayList<String>(); 
        collect.add("A"); 
        collect.add("Computer"); 
        collect.add("Portal"); 
        collect.add("for"); 
        collect.add("Geeks"); 
  
        // Displaying the list 
        System.out.println("The ArrayList is: " + list); 
  
        // Appending the collection to the list 
        list.addAll(collect); 
  
        // displaying the modified ArrayList 
        System.out.println("The new ArrayList is: "
                           + list); 
    } 
}
輸出:
The ArrayList is: []
The new ArrayList is: [A, Computer, Portal, for, Geeks]

示例4:演示NullPointer異常

// Java code to illustrate boolean addAll() 
  
import java.util.*; 
  
public class LinkedListDemo { 
    public static void main(String args[]) 
    { 
  
        // Creating an empty ArrayList 
        Collection<String> 
            list = new ArrayList<String>(); 
  
        // A collection is created 
        Collection<String> collect = null; 
  
        // Displaying the list 
        System.out.println("The ArrayList is: " + list); 
  
        try { 
            // Appending the collection to the list 
            list.addAll(collect); 
        } 
        catch (Exception e) { 
            System.out.println("Exception: " + e); 
        } 
    } 
}
輸出:
The ArrayList is: []
Exception: java.lang.NullPointerException

參考: https://docs.oracle.com/javase/9/docs/api/java/util/Collection.html#addAll-java.util.Collection-



相關用法


注:本文由純淨天空篩選整理自RishabhPrabhu大神的英文原創作品 Collection addAll() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。