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


Java AbstractCollection addAll()用法及代码示例


Java AbstractCollection 的 addAll(Collection C) 方法用于将提到的集合中的所有元素附加到此集合。元素是随机添加的,不遵循任何特定顺序。

用法:

boolean addAll(Collection C)

参数:参数 C 是要添加到集合中的任何类型的集合。

返回值:如果成功地将集合 C 的元素附加到现有集合,则该方法返回 true,否则返回 False。

以下示例程序旨在说明 AbstractCollection.addAll() 方法:



程序1:


// Java code to illustrate addAll()
// method of AbstractCollection
  
import java.util.*;
import java.util.AbstractCollection;
  
public class AbstractCollectionDemo {
    public static void main(String args[])
    {
  
        // Creating an empty collection
        AbstractCollection<String>
            abs1 = new TreeSet<String>();
  
        // Use add() method to add
        // elements into the Collection
        abs1.add("Welcome");
        abs1.add("To");
        abs1.add("Geeks");
        abs1.add("4");
        abs1.add("Geeks");
        abs1.add("TreeSet");
  
        // Displaying the Collection
        System.out.println("AbstractCollection 1:"
                           + abs1);
  
        // Creating anothe Collection
        AbstractCollection<String>
            abs2 = new TreeSet<String>();
  
        // Displaying the Collection
        System.out.println("AbstractCollection 2:"
                           + abs2);
  
        // Using addAll() method to Append
        abs2.addAll(abs1);
  
        // Displaying the Collection
        System.out.println("AbstractCollection 2:"
                           + abs2);
    }
}
输出:
AbstractCollection 1:[4, Geeks, To, TreeSet, Welcome]
AbstractCollection 2:[]
AbstractCollection 2:[4, Geeks, To, TreeSet, Welcome]

程序2:


// Java code to illustrate addAll()
// method of AbstractCollection
  
import java.util.*;
import java.util.AbstractCollection;
  
public class AbstractCollectionDemo {
    public static void main(String args[])
    {
  
        // Creating an empty collection
        AbstractCollection<Integer>
            abs1 = new TreeSet<Integer>();
  
        // Use add() method to add
        // elements into the Collection
        abs1.add(10);
        abs1.add(20);
        abs1.add(30);
        abs1.add(40);
        abs1.add(50);
  
        // Displaying the Collection
        System.out.println("AbstractCollection 1:"
                           + abs1);
  
        // Creating anothe Collection
        AbstractCollection<Integer>
            abs2 = new TreeSet<Integer>();
  
        // Displaying the Collection
        System.out.println("AbstractCollection 2:"
                           + abs2);
  
        // Using addAll() method to Append
        abs2.addAll(abs1);
  
        // Displaying the Collection
        System.out.println("AbstractCollection 2:"
                           + abs2);
    }
}
输出:
AbstractCollection 1:[10, 20, 30, 40, 50]
AbstractCollection 2:[]
AbstractCollection 2:[10, 20, 30, 40, 50]




相关用法


注:本文由纯净天空筛选整理自Chinmoy Lenka大神的英文原创作品 AbstractCollection addAll() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。