ArrayList in java

ArrayList in java is one of the most important parts of the Collection framework. It is the collection that is widely used in programs. Before move further we will recommend you please read Array in java, because that will help you to understand the concept of ArrayList. In this post, we will discuss what is the java ArrayList class and how we can use it.

Here is the table content of the article we will cover these topics.

1. What is ArrayList in java?
2. Important points about ArrayList in java?
3. Creating an ArrayList in java?
4. How to Initialize arraylist java?

5. Internal working of ArrayList in java?
6. How to add elements to an ArrayList?
7. How to replace element in an ArrayList?
8. How to remove elements from an ArrayList?
9. How to access elements from an ArrayList?
10. How to get index of object in ArrayList java?
11. How to Iterate an ArrayList?
12. How to get subList from ArrayList?
13. How to achieve Java sort ArrayList?

14. How to Convert list to array in java?
15. How to convert array to list java?
16. Java ArrayList methods?

17. How to remove duplicates from ArrayList?
18. Difference between ArrayList and array?

19. Difference between ArrayList and LinkedList?
20. Immutable List in Java?
21. CopyOnWriteArrayList in java

What is ArrayList in Java?

The ArrayList class is a part of the Java Collection Framework that implements the List interface and extends the AbstractList class. It also implements the Cloneable, RandomAccess, and Serializable interface.

arraylist in java

As we have seen the ArrayList class implements the List interface, It means it inherits all the properties from the List interface. An ArrayList is a part of collection framework that can store any number of elements/objects. Basically, ArrayList is the growable array, unlike Array, it doesn’t have a fixed length. It means the size of the ArrayList can grow or shrink automatically when the object is added or removed. The ArrayList uses the index to store the element like Array. We will read how Array increase or decrease size automatically.

Important points about ArrayList 

1. Like Array, ArrayList also work based on an index. Each element stores in a particular position. We can add, get, remove the element by use of index value.

2. ArrayList can contain duplicate elements Because it stores each element at a particular position by use of the index. So it doesn’t care whether the element is already present in ArrayList or not.

3. It maintains the order of insertion because each element is inserted on its index value.
For example: If you are adding any element in ArrayList in the First position (it means index value 0). So, you can get it anywhere in the program by the use of index value 0, It doesn’t change automatically.

4. An ArrayList can contain any number of null values. In java, some collections are not allowed null like TreeSet.

5. By default, the ArrayList class is non synchronized. It means multiple threads can access it at same time. We can make it synchronized by using the synchronized keyword or we can use CopyOnWriteArrayList for multithreading.

6. The initial default capacity of ArrayList is 10.

7. The ArrayList always used for non- primitive data types. We can’t be used for primitive types (like int, char, etc).
8. Java ArrayList allows us to randomly access the list. We can get any element from ArrayList based on the index value.

Creating an ArrayList in java?

To create an ArrayList we can use the different constructors of ArrayList. As we know ArrayList class is generic in java so we need to provide the data type. Let’s see how to create arraylist java and how can add elements in ArrayList.

There are three constructors that are used to create an ArrayList. We will cover all the three constructors and also see the difference.
ArrayList() – Create ArrayList with default capacity
ArrayList(size) – Create ArrayList of specific size
ArrayList(Collection) – Creating an ArrayList from another collection

ArrayList()

The default constructor is used to create the default size of ArrayList in java. The ArrayList default size is 10.

ArrayList<E> nameOfArratList = new ArrayList<E>();

Here E denotes the Generic type, We can use the non primitive data type here.

ArrayList<String> names = new ArrayList<String>();

Here names are the name of ArrayList that can store elements of String type. Here we are using the default constructor of ArrayList to create an ArrayList. The ArrayList default size is 10 but it increases when we add elements.

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

class ArrayListExample {

    public static void main(String[] args) {
        // Creating list using the ArrayList class
        List<Integer> arrayListNumber = new ArrayList<Integer>();

        // Add elements to the list
        arrayListNumber.add(123);
        arrayListNumber.add(456);
        arrayListNumber.add(789);
        System.out.println("List: " + arrayListNumber);
    }
}

Output: List: [123, 456, 789]

ArrayList(int capacity)

It is a parameterized constructor of the ArrayList class. It is used to create an empty ArrayList with the specified capacity. It takes only one parameter of int type.

ArrayList<E> NameOfArrayList =  new ArrayList<E>(intValue);

Where, E represents the type of elements in ArrayList.
IllegalArgumentException, if the initial capacity is less than zero

ArrayList<String> listOfNames =  new ArrayList<String>(5);

Let say you want to create an ArrayList and specify its capacity. If you already know about your data capacity it would be better to declare an ArrayList with capacity.

import java.util.ArrayList;

public class ExampleOfArrayList 
{
        public static void main(String[] args) 
	{
		ArrayList<String> listOfNames = new ArrayList<String>(5);
		listOfNames.add("JAVA");
		listOfNames.add("GOAL");
		listOfNames.add("JAVA");
		listOfNames.add("GOAL");
                for(String name : listOfNames)
			System.out.println(name);
	}
}

Output: JAVA
GOAL
JAVA
GOAL

ArrayList(Collection c)

It is a parameterized constructor of the ArrayList class. It is used to create an ArrayList containing the elements in the specified collection.  The ArrayList is created with default initial capacity enough to contain the elements in the specified collection.

ArrayList<E> NameOfArrayList = new ArrayList<E>(collectionName);

Where, E represents the type of elements in ArrayList.ArrayList<String> listOfNames2 = new ArrayList<String>( listOfNames);

Let say you want to create an ArrayList by use of existing collection:

import java.util.ArrayList;
public class ExampleOfArrayList 
{ 
	public static void main(String[] args) 
	{ 
		ArrayList<String> listOfNames = new ArrayList<String>(); 
		listOfNames.add("JAVA"); 
		listOfNames.add("GOAL"); 
		listOfNames.add("SITE"); 
		ArrayList<String> listOfNames2 = new ArrayList<String>(listOfNames); 
		for(String name : listOfNames2) 
			System.out.println(name); 
	}
}

Output:JAVA
GOAL
SITE

How to Initialize ArrayList java?

We can initialize the ArrayList at the time of creation or after creation. Let’s see how we can initialize the ArrayList in java.

ArrayList<data_type>arraylistName = new ArrayList<data_type>()
{{
	add(Object o1); 
        add (Object o2);
        …
	add (Object n);
}};

Here you can see, we can add the elements in ArrayList during the creation of ArrayList.

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

class ArrayListExample {

    public static void main(String[] args) 
    {
    	ArrayList<String> arraylistName = new ArrayList<String>()
    	{{
    		add("Hi"); 
            add("Hello");
            add("Bye");
    	}};
    	
    	System.out.println("Elements of ArrayList : "+arraylistName);
    }
}

Output: Elements of ArrayList : [Hi, Hello, Bye]

Leave a Comment