Java AbstractCollection addAll()方法及示例

Java AbstractCollection addAll()方法及示例

Java AbstractCollectionaddAll(Collection C) 方法用于将一个提到的集合中的所有元素追加到这个 集合中。这些元素是随机添加的,不遵循任何特定的顺序。

语法

boolean addAll( _Collection C)_

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

返回值: 如果该方法成功地将集合 C 的元素追加到现有的集合中,则返回真,否则返回假。下面的程序说明了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 another 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 another 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]

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程