ArrayList in Java is most frequently used collection class after HashMap in Java. Let’s see how many methods in ArrayList is available. Java ArrayList represents an automatic re-sizable array and used in place of the array.
In this tutorial, we will see the different examples and uses of ArrayList in Java.
How to Create an ArrayList
We already have seen, How can we create ArrayList in Java. We have seen how can we create ArrayList with Generics or without Generics.
In the very first tutorial of ArrayList, we have discussed this topic . Let’s see this one more time-
ArrayList<String> stringList = new ArrayList<String> ; // Generic ArrayList to store only String
ArrayList<String> stringList = new ArrayList<>(); // Using Diamond operator from Java 1.5
How to add element into ArrayList
We have seen, how can we add the elements into ArrayList. We can add the elements into ArrayList using add() method.
How to find size of ArrayList
The size of an ArrayList in Java is a total number of elements currently stored in ArrayList. You can easily find a number of elements in ArrayList by calling size() method on it.
int size = stringList.size();
Checking Index of an Item in Java ArrayList
You can use indexOf() method of ArrayList in Java to find out index of a particular object.
Products from Amazon.in
Price: Check on Amazon
Price: INR 29,950.00
Price: INR 15,490.00
int index = stringList.indexOf(“Item”); //location of Item object in List
How to search in ArrayList for an Element
To find the element in ArrayList you can use contains() method of Java. This method returns boolean value, it returns ‘true’ if element present else ‘false’.
boolean isPresent = stringList.contains(“Item”);
How to check if ArrayList is Empty in Java
We can use isEmpty() method of Java ArrayList to check whether ArrayList is empty. isEmpty() method returns true if this ArrayList contains no elements. You can also use size() method of List to check if List is empty or not. If returned size is zero then ArrayList is empty.
boolean result = stringList.isEmpty(); //isEmpty() will return true if List is empty
How to remove an element from ArrayList
There are two ways to remove any elements from ArrayList in Java. You can either remove an element based on its index or by providing object itself. Remove(int index) and remove(Object o) method is used to remove any element from ArrayList.
Copying data from one ArrayList to another ArrayList in Java
Many times you need to create a copy of ArrayList for this purpose you can use an addAll(Collection c) method of ArrayList in Java to copy all elements from on ArrayList to another ArrayList.
ArrayList<String> copyOfStringList = new ArrayList<String>();
Replace an element at a particular index in ArrayList
You can use the set (int index, E element) method of Java ArrayList to replace any element from a particular index. Below code will replace the first element of stringList from “Item” to “Item2”.
Remove all elements from ArrayList
ArrayList in Java provides the clear() method which removes all of the elements from this list.
So these all above methods are the important method of the ArrayList class. Now let’s see some important facts about ArrayList.
- ArrayList is not a synchronized collection, hence, it is not suitable to be used by multiple threads concurrently. If you want to use ArrayList like data-structure in the multi-threaded environment, then you need to either use new CopyonWriteArrayList or use Collections.synchronizedList() to create a synchronized List. Former is part of concurrent collection package and much more scalable than the second one, but only useful when there are many readers and only a few writes.
- CopyOnWriteArrayList is recommended for the concurrent multi-threading environment as it is optimized for multiple concurrent reads and creates copy for the write operation.
- The size(), isEmpty(), get(), set(), iterator(), and listIterator() operations run in constant time because ArrayList is based on Array but adding or removing an element is costly as compared to LinkedList.
- Since ArrayList implements List interface it maintains insertion order of elements and also allow duplicates.
- If we set the ArrayList reference to null in Java, all the elements inside ArrayList become eligible to garbage collection in Java.
So we have discussed almost all important facts about arrayList. If you have any doubt, please write us we will try our best to solve your doubts.
Selenium Tutorials :
If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.