The ensureCapacity(int minCapacity) method of java.util.ArrayList class increases the capacity of this ArrayList instance if necessary to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
Swapping the elements of the ArrayList
- Create a new arraylist arraylist al1.
- Add elements to al1 using add() method.
- Use Collections.swap() method to swap the required elements of al1.
public boolean removeAll(Collection<?> c)
It removes all the elements in the list that are not contained in the specified list. [Read more…]
We all know that ArrayList class is non-synchronized and should not be used in multi-thread environment without explicit synchronization. In this article we will learn how to synchronize ArrayList in Java. We will be following two approaches here
- Using Collections.synchronizedList() method
- Using CopyOnWriteArrayList (thread-safe variant of ArrayList)