当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。