Collections

Java arraylist add method example

This tutorial shows you how to add elements to arraylist in different ways.

How to add to elements in arraylist java – add(E e)

The below example uses add method from java API to append or add an element to the end of the ArrayList

package com.candidjava.arrayList;

import java.util.ArrayList;

public class ArrayListAdd 
{
	public static void main(String[] args) {
		
		ArrayList<String> al=new ArrayList<String>();
		
		// add first element to arraylist
		al.add("candid");		
		System.out.println(al); // print arraylist data

		// add more elements to arraylist
		al.add("mathan");
		al.add("antony");
		System.out.println(al);
		
	}
}

Output

[candid]
[candid, mathan, antony]

Add elements to specified positions in arraylist java – add(int index, E element)

package com.candidjava.arrayList;

import java.util.ArrayList;

public class ArrayListAddIndex 
{
	public static void main(String[] args) {
		
		ArrayList<String> al=new ArrayList<String>();

		al.add("candid");		
		al.add("mathan");
		al.add("antony");
		System.out.println(al);
		
		// adding element to 1st index position in arraylist
		al.add(1, "kumar");
		
		System.out.println("After adding elements in specific index");
		System.out.println(al);
		
	}
}

Output

[candid, mathan, antony]
After adding elements in specific index
[candid, kumar, mathan, antony]

How to add arraylist to another arraylist in java – addAll(Collection c)

package com.candidjava.arrayList;

import java.util.ArrayList;

public class ArrayListAddArray 
{
	public static void main(String[] args) {
		
		ArrayList<String> al1=new ArrayList<String>();

		al1.add("candid");		
		al1.add("mathan");
		al1.add("antony");
	
		System.out.println("First arraylist ");
		System.out.println(al1);
		
		
		ArrayList<String> al2=new ArrayList<String>();

		al2.add("chennai");		
		al2.add("tiruchy");
		al2.add("madurai");
	
		System.out.println("Second arraylist ");
		System.out.println(al2);
		
		// adding arraylist 1 to arraylist 2 using addAll
		al2.addAll(al1);
		System.out.println("After adding arraylist 1 to arrayList 2");
		System.out.println(al2);	
	}
}

Output

First arraylist 
[candid, mathan, antony]
Second arraylist 
[chennai, tiruchy, madurai]
After adding arraylist 1 to arrayList 2
[chennai, tiruchy, madurai, candid, mathan, antony]

How to add array in arraylist in java

package com.candidjava;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ArrayListAddArray {
	public static void main(String[] args) {
		String[] names = { "teena", "reena", "meena", "ram", "sam" };
		
               // Using arrays.aslist()
		List l = new ArrayList<String>(Arrays.asList(names));
		System.out.println("Adding into arraylist using arrays.aslist:");
		System.out.println(l);
		
                // Using Collections.addall()
		List l1 = new ArrayList<String>();
		Collections.addAll(l1, names);
		System.out.println("Adding into arraylist using collection.addall:");
		System.out.println(l1);
		
                // Adding elements to an already existing array
		List l2 = new ArrayList<String>();
		Collections.addAll(l2, names);
		boolean b = Collections.addAll(l2, "Rahul", "OM", "Prem");
		System.out.println("Adding elements into list l2:");
		System.out.println(l2);
		
                // Adding elements to array
		l1.add("krish");
		l1.add("jack");
		System.out.println("Adding elements into list l1:");
		System.out.println(l1);

	}
}

Output

Adding into arraylist using arrays.aslist:
[teena, reena, meena, ram, sam]
Adding into arraylist using collection.addall:
[teena, reena, meena, ram, sam]
Adding elements into list l2:
[teena, reena, meena, ram, sam, Rahul, OM, Prem]
Adding elements into list l1:
[teena, reena, meena, ram, sam, krish, jack]

How to add int array to arraylist in java

package com.candidjava;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ArrayListAddIntArray {
	public static void main(String[] args) {
		Integer[] num = { 12, 33, 44, 55, 66, 77 };
		// Using Arrays.aslist
		List l = new ArrayList<Integer>(Arrays.asList(num));
		System.out.println("adding using arrays.aslist:");
		System.out.println(l);
		//Using Collections.addall
		List l1=new ArrayList<Integer>();
		Collections.addAll(l1,num);
		System.out.println("adding using collections.addall:");
		System.out.println(l1);
		//
		int[] n=new int[]{22,33,44,55};
		List<int[]> l2 = new ArrayList<>();
		l2.add(n);
		System.out.println(l2.get(0)[0]);
		System.out.println(l2.get(0)[1]);
		System.out.println(l2.get(0)[2]);
		System.out.println(l2.get(0)[3]);
	}
}

Output

adding using arrays.aslist:
[12, 33, 44, 55, 66, 77]
adding using collections.addall:
[12, 33, 44, 55, 66, 77]
22
33
44
55

How to add class object to arraylist in java

package com.candidjava;

import java.util.ArrayList;
import java.util.Iterator;

class Student {
	int rollno;
	String name;
	int age;

	Student(int rollno, String name, int age) {
		this.rollno = rollno;
		this.name = name;
		this.age = age;
	}

	public int getRollno() {
		return rollno;
	}

	public void setRollno(int rollno) {
		this.rollno = rollno;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

}

public class ArraylistaddObject {
	public static void main(String[] args) {

		Student s1 = new Student(101, "kavi", 15);
		Student s2 = new Student(102, "ram", 15);
		Student s3 = new Student(103, "sam", 15);
		// Adding objects into array list
		ArrayList<Student> al = new ArrayList<Student>();
		al.add(s1);
		al.add(s2);
		al.add(s3);
		Iterator itr = al.iterator();
		while (itr.hasNext()) {
			Student st = (Student) itr.next();
			System.out.println(st.getRollno() + " " + st.getName() + " " + st.getAge());
		}

	}

}

Output

101 kavi 15
102 ram 15
103 sam 15

How to add hashmap values to arraylist in java

package com.candidjava;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Set;

public class ArrayListAddHashMap {
	public static void main(String[] args) {
		HashMap<Integer, String> hm = new HashMap<>();
		hm.put(101, "Remo");
		hm.put(102, "Rio");
		hm.put(103, "Ram");
		//Adding key into arraylist
		Set<Integer> ks = hm.keySet();
		ArrayList<Integer> al=new ArrayList<Integer>(ks);
		System.out.println(al);
		//Adding Values into arraylist
		Collection<String> value=hm.values();
		ArrayList<String> al1=new ArrayList<String>(value);
		System.out.println(al1);
        
	}

}

Output

[101, 102, 103]
[Remo, Rio, Ram]