ArrayList in java is most common Collections data structure along with
HashMap which we use very often.
Why to choose ArrayList vs Array:
- Array is fixed length data structure If array is full , you can not add element to it, where as ArrayList in java can dynamically grow and shrink as per our need.
- You can use generics with ArrayList but not with Array
- ArrayList have predefined methods which can be used to perform operations.
Some important points about ArrayList in java are:
- ArrayList is implementation of list interface.
- ArrayList is not synchonized(so not thread safe)
- ArrayList is implemented using array as internal data structure.It can be dynamically resized .
- ArrayList increases half of its size when its size is increased.
Example of ArrayList in java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
package org.arpit.java2blog;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.*;
public class JavaArrayListExampleMain {
/*
* @author : Arpit Mandliya
*/
public static void main(String[] args) {
ArrayList employeeNameList = new ArrayList();
employeeNameList.add("John");
employeeNameList.add("Ankit");
employeeNameList.add("Rohan");
employeeNameList.add("Amit");
System.out.println("Employee list:");
for (String empName : employeeNameList) {
System.out.println(empName);
}
}
}
|
When you run above program, you will get below output:
|
Employee list:
John
Ankit
Rohan
Amit
|
Iterate over list :
We have used foreach loop to iterate over list in above example: There are many ways to iterate over list. Some of them are:
- Using for loop
- Using Iterator
Iterator example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
package org.arpit.java2blog;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.*;
public class JavaArrayListExampleMain {
/*
* @author : Arpit Mandliya
*/
public static void main(String[] args) {
ArrayList employeeNameList = new ArrayList();
employeeNameList.add("John");
employeeNameList.add("Ankit");
employeeNameList.add("Rohan");
employeeNameList.add("Amit");
System.out.println("Employee list:");
Iterator iter = employeeNameList.iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
}
}
|
When you run above program, you will get below output:
|
Employee list:
John
Ankit
Rohan
Amit
|
Methods of ArrayList in java
I am explaining below method in context of String list, but you can put any object in the list.
add( Object o): This method adds an object o to end of the arraylist.
|
employeeNameList.add("Amit")
|
This statement would add a string Amit in the ArrayList at last position.
add(int index, Object o): This adds the object o to the arraylist at the specified index.
|
employeeNameList.add(5,"Ankit")
|
It will add the string Ankit to the 5th index (6rd position as the array list starts with index 0) of ArrayList.
Object get(int index): It returns the object of list which is present at the specified index.
|
String str= employeeNameList.get(2);
|
It is used to get element from ArrayList and this is most used method. Here we are getting element from ArrayList and assigning it to String object
remove(Object o): Removes the object o from the ArrayList.
|
employeeNameList.remove("Arpit");
|
This code will remove the string “Arpit” from the ArrayList.
remove(int index): Removes element which is present at specified index
|
employeeNameList.remove(3);
|
It would remove the element of index 3 (4th element of the list as list always starts with 0).
set(int index, Object o): It is mainly used for updating an element. It replaces the element present at the specified index with the object o.
|
employeeNameList.set(2, "Ankit");
|
It would replace element which is present at index 2(3rd element in arrayList)with the value Tom.
|
int index = employeeNameList.indexOf("Ankit");
|
This would give the index (position) of the string Ankit in employeeNameList.
int size(): It gives the
size of the ArrayList
|
int sizeOfArrayList = employeeNameList.size();
|
boolean contains(Object o):
It checks whether the given object o is present in the ArrayList or not. If not present, it returns false
|
employeeNameList.contains("Amit");
|
It would return true if the string “Amit” is present in the list else it will return false
clear():
It will remove all the object of ArrayList, so above statement will remove all String object from employeeNameList
addAll(Collection c)
If you want to merge two ArrayList, you can use this method.
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
package org.arpit.java2blog;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.*;
public class JavaArrayListExampleMain {
/*
* @author : Arpit Mandliya
*/
public static void main(String[] args) {
ArrayList employeeNameList = new ArrayList();
employeeNameList.add("John");
employeeNameList.add("Ankit");
employeeNameList.add("Rohan");
employeeNameList.add("Amit");
ArrayList otherList = new ArrayList();
otherList.add("abc");
otherList.add("xyz");
// Adding otherList to employeeList
employeeNameList.addAll(otherList);
System.out.println("Employee list:");
for (String empName : employeeNameList) {
System.out.println(empName);
}
}
}
|
When you run above program, you will get below output:
|
Employee list:
John
Ankit
Rohan
Amit
abc
xyz
|
removeAll(Collection c)
If you want to remove list of elements which is present in other list, you can use this method.
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
package org.arpit.java2blog;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.*;
public class JavaArrayListExampleMain {
/*
* @author : Arpit Mandliya
*/
public static void main(String[] args) {
ArrayList employeeNameList = new ArrayList();
employeeNameList.add("John");
employeeNameList.add("Ankit");
employeeNameList.add("Rohan");
employeeNameList.add("Amit");
ArrayList otherList = new ArrayList();
otherList.add("John");
otherList.add("Rohan");
// removing otherList's element from ArrayList
employeeNameList.removeAll(otherList);
System.out.println("Employee list:");
for (String empName : employeeNameList) {
System.out.println(empName);
}
}
}
|
When you run above program, you will get below output:
|
Employee list:
Ankit
Amit
|
retainAll(Collection)
If you want to retain only those elements which are present in Collection, you can use this method.
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
package org.arpit.java2blog;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.*;
public class JavaArrayListExampleMain {
/*
* @author : Arpit Mandliya
*/
public static void main(String[] args) {
ArrayList employeeNameList = new ArrayList();
employeeNameList.add("John");
employeeNameList.add("Ankit");
employeeNameList.add("Rohan");
employeeNameList.add("Amit");
ArrayList otherList = new ArrayList();
otherList.add("John");
otherList.add("Rohan");
// removing otherList's element from ArrayList
employeeNameList.retainAll(otherList);
System.out.println("Employee list:");
for (String empName : employeeNameList) {
System.out.println(empName);
}
}
}
|
When you run above program, you will get below output:
|
Employee list:
John
Rohan
|