Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Monday, October 16, 2017

Add ArrayList to another ArrayList in Java | addAll method

In this post, We will learn How to add ArrayList into a another ArrayList in java.

We can accumulate this in two ways.

1) While creating ArrayList object.
2) Using addAll method.


Add ArrayList to another ArrayList in Java


1) Adding existing ArrayList into new list:

ArrayList has a constructor which takes list as input. Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

ArrayList(Collection c)


Example Program:


package com.adeepdrive.arraylist;

import java.util.ArrayList;
import java.util.List;
// Java - W3schools
public class AddAnotherList 
{
    public static void main(String[] args) 
    {
        // Creating ArryList - Existing Object
        List<String> existingList = new ArrayList<String>();
        existingList.add("America");
        existingList.add("New York");
        existingList.add("Sidny");

        System.out.println("Existing ArrayList Object : " + existingList);
        // Creating a new ArrayList by passing exiting ArrayList
        List<String> newList = new ArrayList<String>(existingList);

        // Printng the new List
        System.out.println("Newly created ArrayList Object : " + newList);
    }
}

Output:

Existing ArrayList Object : [America, New York, Sidny]
Newly created ArrayList Object : [America, New York, Sidny]

Explanation:

First created one ArrayList object as named existingList and added three values to them as America, New York, Sidny. Then printed values of existingList.
Next created another ArrayList object as newList by passing existing existingList and not added any values to it, just printed newList then we see the same values as existingList.

2. How to add using addAll method:

ArrayList has a method named addAll() which is very useful when we do not have access arraylist creation logic (creation implementation is in some jar - third party jar).

public boolean addAll(Collection c)

addAll Example program:


package com.adeepdrive.arraylist;

import java.util.ArrayList;
import java.util.List;

// Java - W3schools
public class AddAnotherListAddAll
{
    public static void main(String[] args) 
    {
       // getting existing Roles.
         List<String> existingRoles = getExistingRoles();
        // We have new roles in the list.

        List<String> newUserRoles = new ArrayList<String>();
        newUserRoles.add("TimesheetApprove");
        newUserRoles.add("LeaveApprove");
        newUserRoles.add("CabApprove");


        // We have to add new roles to the existing roles then we should go for addAll
        // method.

        System.out.println("Printing roles before adding new roles: "+existingRoles);
        existingRoles.addAll(newUserRoles);
        System.out.println("Printing roles after adding new roles: "+existingRoles);
    }

    // For understanding, I have created new method in the same class. In real time,
    // this method will be in some jar which is provided by other application
    // managed by different team.

    public static List<String> getExistingRoles() 
    {
        // Creating ArryList - Existing Object
        List<String> existingRoles = new ArrayList<String>();
        existingRoles.add("ReadOnly");
        existingRoles.add("EmployeLevel");
        existingRoles.add("MonitierLevel");

        return existingRoles;
    }
}

Output:

Printing roles before adding new roles: [ReadOnly, EmployeLevel, MonitierLevel]
Printing roles after adding new roles: [ReadOnly, EmployeLevel, MonitierLevel, TimesheetApprove, LeaveApprove, CabApprove]

Explanation:

Please see the comments given in the above program.
First got all existing roles by calling getExistingRoles method. Then, we have created another new ArrayList object then added new roles to it.
At this point we have two list and values as below.

existingRoles: [ReadOnly, EmployeLevel, MonitierLevel]
newUserRoles: [TimesheetApprove, LeaveApprove, CabApprove]

Next, we want to add newUserRoles to the existingRoles object.

existingRoles.addAll(newUserRoles);

Now observe before and after adding new roles in existingRoles.

No comments:

Post a Comment

Please do not add any spam links in the comments section.