Home  >  Article  >  Java  >  Java adds one collection to another collection using addAll() function of ArrayList class

Java adds one collection to another collection using addAll() function of ArrayList class

WBOY
WBOYOriginal
2023-07-24 22:01:512157browse

Java uses the addAll() function of the ArrayList class to add one collection to another collection

In Java programming, it is often necessary to add elements from one collection to another collection. In order to achieve this function, we can use the addAll() function of the ArrayList class. This function adds a collection to the end of another collection and returns the added collection.

ArrayList is a commonly used implementation class in the Java collection framework. It implements the List interface and can store any type of object. ArrayList stores elements in the form of dynamic arrays. It has an automatic expansion function and can automatically adjust the size as needed.

The syntax of the addAll() function is as follows:

public boolean addAll(Collection2d4902c92e1e7bfd574f59708c57776a c)

In this syntax, the addAll() function receives an implementation The collection of the Collection interface is passed as a parameter and all its elements are added to the ArrayList calling the method. The return value is a Boolean type, indicating whether the collection has changed. If an element has been added, it returns true, otherwise it returns false.

Let's look at an example that demonstrates how to use the addAll() function to add a collection to another collection:

import java.util.ArrayList;

public class ArrayListAddAllExample {
    public static void main(String[] args) {
        // 创建第一个ArrayList
        ArrayList<String> list1 = new ArrayList<>();
        list1.add("apple");
        list1.add("banana");
        list1.add("cherry");

        // 创建第二个ArrayList
        ArrayList<String> list2 = new ArrayList<>();
        list2.add("mango");
        list2.add("orange");

        // 使用addAll()函数将第二个集合添加到第一个集合中
        list1.addAll(list2);

        // 打印添加后的第一个集合
        System.out.println("添加后的第一个集合:");
        for (String fruit : list1) {
            System.out.println(fruit);
        }
    }
}

Run the above code, you will get the following output:

添加后的第一个集合:
apple
banana
cherry
mango
orange

In the example, we first created two ArrayList objects, list1 and list2. Then use the addAll() function to add the elements in list2 to list1. Finally, by traversing list1, print out all the elements in the added collection.

It is worth noting that the addAll() function can not only be used to add a collection to an ArrayList, but can also be used to add multiple collections to an ArrayList in order. If the same elements exist, the addAll() function will also add these duplicate elements to the ArrayList.

Summary: In Java, you can easily add a collection to another collection using the addAll() function of the ArrayList class. This function can merge collections with a simple line of code. Using the addAll() function can improve the readability and maintainability of the code, and is a very common method when dealing with collection operations.

The above is the detailed content of Java adds one collection to another collection using addAll() function of ArrayList class. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn