List
The List interface provides a way to store the ordered collection. It is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements.
Source: https://www.geeksforgeeks.org/list-interface-java-examples
List in Java provides the facility to maintain the ordered collection. It contains the index-based methods to insert, update, delete and search the elements. It can have the duplicate elements also. We can also store the null elements in the list.
The List interface is found in the java.util package and inherits the Collection interface. It is a factory of ListIterator interface. Through the ListIterator, we can iterate the list in forward and backward directions. The implementation classes of List interface are ArrayList, LinkedList, Stack and Vector. The ArrayList and LinkedList are widely used in Java programming. The Vector class is deprecated since Java 5.
Source: https://www.javatpoint.com/java-list
ArrayList
Java ArrayList class uses a dynamic array for storing the elements. It is like an array, but there is no size limit. We can add or remove elements anytime. So, it is much more flexible than the traditional array. It is found in the java.util package
It inherits the AbstractList class and implements List interface.
The important points about Java ArrayList class are:
- Java ArrayList class can contain duplicate elements.
- Java ArrayList class maintains insertion order.
- Java ArrayList allows random access because array works at the index basis.
ArrayList Hierarchy
Source: https://www.javatpoint.com/java-arraylist
ArrayList declaration
ArrayList l=new ArrayList();
Similarities in Array and ArrayList
- Array and ArrayList both are used for storing elements.
- Array and ArrayList both can store null values.
- They can have duplicate values.
- They do not preserve the order of elements.
Difference between Array and ArrayList
Basis | Array | ArrayList |
---|---|---|
Definition | An array is a dynamically-created object. It serves as a container that holds the constant number of values of the same type. It has a contiguous memory location. | The ArrayList is a class of Java Collections framework. It contains popular classes like Vector, HashTable, and HashMap. |
Static/ Dynamic | Array is static in size. | ArrayList is dynamic in size. |
Resizable | An array is a fixed-length data structure. | ArrayList is a variable-length data structure. It can be resized itself when needed. |
Initialization | It is mandatory to provide the size of an array while initializing it directly or indirectly. | We can create an instance of ArrayList without specifying its size. Java creates ArrayList of default size. |
Performance | It performs fast in comparison to ArrayList because of fixed size. | ArrayList is internally backed by the array in Java. The resize operation in ArrayList slows down the performance. |
Primitive/ Generic type | An array can store both objects and primitives type. | We cannot store primitive type in ArrayList. It automatically converts primitive type to object. |
Iterating Values | We use for loop or for each loop to iterate over an array. | We use an iterator to iterate over ArrayList. |
Type-Safety | We cannot use generics along with array because it is not a convertible type of array. | ArrayList allows us to store only generic/ type, that’s why it is type-safe. |
Length | Array provides a length variable which denotes the length of an array. | ArrayList provides the size() method to determine the size of ArrayList. |
Adding Elements | We can add elements in an array by using the assignment operator. | Java provides the add() method to add elements in the ArrayList. |
Single/ Multi-Dimensional | Array can be multi-dimensional. | ArrayList is always single-dimensional. |
Source: https://www.javatpoint.com/difference-between-array-and-arraylist
Methods used in ArrayList
1) add(Object e): Add the element at the end of the list. The return datatype should be boolean.
Example
l.add(10);
l.add(20);
l.add(20.5f);
l.add(‘a’);
l.add(“name”);
System.out.println(l);
Output
[10, 20, 20.5, a, name]
2) add(int index, Object e): Add the element in specified index position in ArrayList. The return datatype should be void.
Example
ArrayList l = new ArrayList();
l.add(10);
l.add(20);
l.add(20.5f);
l.add(‘a’);
l.add(“name”);
l.add(1, “30.5”);
System.out.println(l);
Output
[10, 30.5, 20, 20.5, a, name]
3) addAll(Collection c): Add all element of specified collection to the end of the list. The return datatype should be boolean.
Example
ArrayList l = new ArrayList();
l.add(10);
l.add(20);
l.add(20.5f);
l.add(1, “30.5”);
ArrayList l2=new ArrayList();
l2.add(‘a’);
l2.add(30);
l2.add(“hello”);
l2.addAll(l);
System.out.println(l2);
Output
[a, 30, hello, 10, 30.5, 20, 20.5]
4) addAll(int index,Collection c): Add all element of specified collection to the specified index position of the ArrayList. The return datatype should be boolean.
Example
ArrayList l = new ArrayList();
l.add(10);
l.add(20);
l.add(20.5f);
l.add(1, “30.5”);
ArrayList l2=new ArrayList();
l2.add(‘a’);
l2.add(30);
l2.add(“hello”);
l2.addAll(0, l);
System.out.println(l2);
Output
[10, 30.5, 20, 20.5, a, 30, hello]
5) size(): Returns the number of element in the list. The return datatype should be int.
Example
ArrayList l = new ArrayList();
l.add(10);
l.add(20);
l.add(20.5f);
l.add(1, “30.5”);
System.out.println(l.size());
Output
4
6) contains(Object e): Return true if the specified element is present in the list. The return datatype should be boolean.
Example
ArrayList l = new ArrayList();
l.add(10);
l.add(20);
l.add(20.5f);
l.add(1, “30.5”);
System.out.println(l.contains(20));
System.out.println(l.contains(100));
Output
true
false
7) clear(): Clear all elements present in the Arraylist. The return datatype should be void.
Example
ArrayList l2=new ArrayList();
l2.add(‘a’);
l2.add(30);
l2.add(“hello”);
l2.addAll(0, l);
System.out.println(l2);
l2.clear();
System.out.println(l2);
Output
[10, 30.5, 20, 20.5, a, 30, hello]
[]
8) ensure Capacity(int minCapacity): Increase the capacity of ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument. The return datatype should be void.
Example
ArrayList l = new ArrayList();
l.ensureCapacity(3);
l.add(10);
l.add(20);
l.add(20.5f);
System.out.println(l);
Output
[10, 20, 20.5]
9) get(int Index): Returns the element at the specified position in this list. The return datatype should be E (type parameter in arraylist).
Example
ArrayList l = new ArrayList();
l.add(10);
l.add(20);
l.add(20.5f);
System.out.println(l.get(0));
Output
10
10) indexOf(Object e): Returns the index of the first occurrence of the specified element in this list. The return datatype should be int.
Example
ArrayList l = new ArrayList();
l.add(10);
l.add(20);
l.add(20);
l.add(20);
System.out.println(l.indexOf(20));
Output
2
11) lastIndexOf(Object e): Returns the index of the last occurrence of the specified element in this list. The return datatype should be int.
Example
ArrayList l = new ArrayList();
l.add(10);
l.add(20);
l.add(20);
l.add(30);
l.add(20);
System.out.println(l.lastIndexOf(20));
Output
4
12) isEmpty(): Return the value true if the list contains no elements. The return datatype should be boolean.
Example
ArrayList l = new ArrayList();
l.add(10);
l.add(20);
l.add(20);
l.add(30);
l.add(20);
System.out.println(l.isEmpty());
ArrayList l2=new ArrayList();
System.out.println(l2.isEmpty());
Output
false
true
13) remove(int Index): Removes the element at the specified position in this list. The return datatype should be E (type parameter in arraylist).
Example
ArrayList l2=new ArrayList();
l2.add(200);
l2.add(‘s’);
l2.add(3.4f);
l2.add(“suji”);
System.out.println(l2 .remove(0) );
Output
[s, 3.4f, suji]
14) remove(Object e): Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. The return datatype should be boolean.
Example
ArrayList l2=new ArrayList();
l2.add(200);
l2.add(‘s’);
l2.add(3.4f);
l2.add(“suji”);
l2.add(true);
System.out.println(l2);
System.out.println(l2.remove(3.4f));
System.out.println(l2.remove(“priya”));
System.out.println(l2);
Output
[200, s, 3.4, suji, true]
true
false
[200, s, suji, true]
15) removeAll(Collection c): Removes from this list all of its elements that are contained in the specified collection. The return datatype should be boolean.
Example
ArrayList l = new ArrayList();
l.add(10);
l.add(20);
l.add(20);
l.add(30);
l.add(20);
System.out.println(l);
ArrayList l2=new ArrayList();
l2.add(200);
l2.add(‘s’);
l2.add(3.4f);
l2.add(“suji”);
l2.add(true);
l2.addAll(l);
System.out.println(l2);
System.out.println(l2.removeAll(l));
System.out.println(l2);
Output
[10, 20, 20, 30, 20]
[200, s, 3.4, suji, true, 10, 20, 20, 30, 20]
true
[200, s, 3.4, suji, true]
16) retainAll(Collection c): Retains only the elements in this list that are contained in the specified collection. The return datatype should be boolean.
ArrayList l = new ArrayList();
l.add(10);
l.add(20);
l.add(20);
l.add(30);
l.add(20);
System.out.println(l);
ArrayList l2=new ArrayList();
l2.add(200);
l2.add(‘s’);
l2.add(3.4f);
l2.add(“suji”);
l2.add(true);
l2.addAll(l);
System.out.println(l2);
System.out.println(l2.retainAll(l));
System.out.println(l2);
Output
[10, 20, 20, 30, 20]
[200, s, 3.4, suji, true, 10, 20, 20, 30, 20]
true
[10, 20, 20, 30, 20]
17) set(int index, Object e): Replaces the element at the specified position in this list with the specified element. The return datatype should be E (type parameter in arraylist).
Example
ArrayList l = new ArrayList();
l.add(10);
l.add(20);
l.add(20);
l.add(30);
l.add(20);
System.out.println(l);
l.set(0, “true”);
System.out.println(l);
Output
[10, 20, 20, 30, 20]
[true, 20, 20, 30, 20]
18) subList(int fromIndex, int toIndex ): Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list isempty). The return datatype should be E (type parameter in arraylist).
Example
ArrayList l = new ArrayList();
l.add(10);
l.add(20);
l.add(20);
l.add(30);
l.add(20);
System.out.println(l);
System.out.println(l.subList(2, 4));
Output
[10, 20, 20, 30, 20]
[20, 30]