Best ArrayList Class examples in java| java array list tutorial
This tutorial explains ArrayList basics in java and features and the top best examples.
ArrayList in java
ArrayList
is the implementation of List
interface in java.util
package.
It stores the collection of objects in the order of insertion.
ArrayList
stores the object and grows the size of the list dynamically
. Due to this, the array list is a resizable array. whereas an array
is static
once assigned the values, the size is fixed.
In real-time programming, ArrayList uses to manipulate the collection of custom objects.
ArrayList is a popular collection framework used by Java developers.
Important points to remember for ArrayList
- The object stored in ArrayList is insertion order
- ArrayList is used to store the collection of objects with size increasing dynamically.
- It is a dynamic array, that increases its size when elements are added, and shrinks size when elements are deleted.
- ArrayList stores the objects that include null values.
- ArrayList is not synchronized
- These are not sorted
Real-time ArrayList Usage examples
In the School management application, You have a list of students stored in the Database. To display those students on the User interface, You have to make a call to the database and store these elements in the collection List and populate those list elements on UI. here List is used as a temporary Object for holding the elements.
- When you are working on CRUD operations of any database entries, ArrayList is used to store the objects in a temporary cache mechanism
How to create an ArrayList
ArrayList has a default and parameterized constructor with an integer parameter.
ArrayList initializes with integer numbers also.
ArrayList emptylist=new ArrayList(); or
ArrayList list=new ArrayList(10);
The above lines of code create an ArrayList object with an empty size as well as the fixed size of the array list. In the second case, the fixed size is 10.
And Another way is ArrayList can also be created and initialized with default values
ArrayList<String> list = new ArrayList<String>() {{
add("one");
add("two");
add("three");
}};
How to add Objects to ArrayList?
ArrayList provides add a method to add the object to ArrayList
Here is an example
ArrayList<String> list=new ArrayList<>();
list.add("One");
list.add("Two");
list.add("Three");
list.add("Four");
list.add("Five");
How to iterate the elements in the ArrayList?
Using for each look, we can iterate each object in the list
for (Object str : list) {
System.out.print(" " + (String) str);
}
and the output is
One Two Three Four Five
Always make sure that you check for null check for the list as it will throw NullPointerException
if the list is null.
Add the following code snippet for avoiding NullPointerException
in ArrayList
if(list!=null){
add the for loop code here
}
How to find the size of ArrayList?
Java ArrayList API provides the size()
method to find the number of elements in the ArrayList
list.size()
Return the size of the list,4 is returned for the above code.
How to check if the object/element is found in ArrayList
ArrayList provides the contains()
method which returns true if a particular object is found in the system
System.out.print(" " + list.contains("One"));
And also another way is to use the indexOf()
method to get the index of an element in ArrayList.
This method returns position if the element is found in
the array list else returns -1 if the element/object is not available in the array list.
How to Convert ArrayList to the array?
As array list to the array is one of the common mechanisms in the day-to-day programming.
Collection API provides the ArrayList.toArray()
method used to convert to an array of objects
String[] strArray=(String [])list.toArray(new String[list.size()]);
for (Object str : strArray) {
System.out.print(" " + (String) str);
}
`
and the output is
One Two Three Four Five
How to convert the array to ArrayList in java
to convert the array
to ArrayList
, collection API provides
java.util.Arrays
class asList()
method which accepts ArrayList
String[] strObjects = {"One1", "two2", "three3", "four4"};
List arrayList= new ArrayList(Arrays.asList(strObjects));
How to remove/delete an element in the list
ArrayList
has to use the remove
method to remove the object from the list.
arrayList.remove("one");
How to get the elements from the list?
The list provides the get()
method to return the object found in the list.
list.get("one")
If the element is not found, null
is returned.
How to sort objects in ArrayList?
If we are displaying the objects from the list, their order is of insertion order only.
To sort the objects in the ArrayList, collection API provides Collections.sort(List) method to sort in the natural order.
Collections.sort(list); // natural order
Collections.reverse(list); // natural order
These elements sort in natural order by the hashcode as well equals the method default implementation order.
If we want to display the custom order for primitive types or objects based on member variables, we have to write our custom comparable
java class which is passed to the Collections.sort() method as another parameter.
Collections.sort(list,Comparable interface);
java8 Iteration list
java8 provides forEach loop with a lambda expression to iterate the elements in a list
list.forEach(item -> System.out.println(item));
I hope you got the basic idea on the array list with examples to work.
Please share your comments on this.