Below is the example of joining the two ArrayList.
ArrayList is an implementation of the List interface. And, List interface has two methods add(), addAll(). These are the two methods which we are going to use in our example.
Output:
User Name List: [Amzi, java]
Password List: [passw0rd, coreJava]
Join both the List: [Amzi, java, passw0rd, coreJava]
ArrayList is an implementation of the List interface. And, List interface has two methods add(), addAll(). These are the two methods which we are going to use in our example.
import java.util.ArrayList;
import java.util.List;
public class JoinList {
/**
* @param args
* @author Amjad Saiyad
*/
public static void main(String[] args) {
List<String> userName = new ArrayList<String>();
userName.add("Amzi");
userName.add("java");
List<String> password = new ArrayList<String>();
password.add("passw0rd");
password.add("coreJava");
List<String> joinBoth = new ArrayList<String>();
joinBoth.addAll(userName);
joinBoth.addAll(password);
System.out.println("User Name List: " + userName);
System.out.println("Password List: " + password);
System.out.println("Join both the List: " + joinBoth);
}
}
Output:
User Name List: [Amzi, java]
Password List: [passw0rd, coreJava]
Join both the List: [Amzi, java, passw0rd, coreJava]
No comments:
Post a Comment