Java ArrayList is an ordered collection aka sequence, implementation of java.util.List interface. ArrayList has the following attributes

  • Backed by dynamic array data structure, its capacity auto grows internally

  • Allow null and duplicate elements

  • Elements are positioned as their insertion order

  • Zero based integer index, can access (get / insert / replace/ remove) elements by their index

  • Unsynchronized implementation. In multi-threading environment with at least one thread modifies the list, it must be synchronized externally

ArrayList declaration and initialization

  • In one line with Arrays.asList
List<Integer> lst = Arrays.asList(1, 2, 3);  
  • In one line with Double brace initialization (should not be used)
List<Integer> lst = new ArrayList<Integer>(){{  
    add(1);
    add(2);
    add(3);
}};

Double brace initialization can lead to memory leaks as it creates an anonymous class with a reference to the owner object

  • In multiple lines
List<Integer> lst = new ArrayList<>();  
lst.add(1);  
lst.add(2);  
lst.add(3);  

CRUD operations of ArrayList elements

All index based operations throw IndexOutOfBoundsException if the index is out of range

  • Appends an element at the end of the list
List<Integer> lst = Arrays.asList(1, 2, 3);  
lst.add(4);  
  • Inserts an element at a specified index in the list and shifts the current and subsequent elements to the right
List<Integer> lst = Arrays.asList(1, 2, 3);  
lst.add(1, 10); // insert 10 at index 1, the list now is {1, 10, 2, 3}  
lst.add(5, 10); // throws `IndexOutOfBoundsException`  
  • Gets an element at a specified index in the list
List<Integer> lst = Arrays.asList(1, 2, 3);  
Integer ele = lst.get(0); // gets element at index 0, returns 1  
Integer invalidEle = lst.get(3); // throws IndexOutOfBoundsException  
  • Replaces an element at a specified index in the list
List<Integer> lst = Arrays.asList(1, 2, 3);  
lst.set(0, 10); // replace element at index 0 with 10, the list now is {10, 2, 3}  
lst.set(3, 10); // throws IndexOutOfBoundsException
  • Removes an element at a specified location in the list and shifts any subsequent elements to the left
List<Integer> lst = Arrays.asList(1, 2, 3);  
lst.remove(0); // remove element at index 0 from the list, the list now is {2, 3}  
lst.remove(3); // throws IndexOutOfBoundsException

ArrayList traversal, sorting and conversion

ArrayList traversal

List<Integer> lst = Arrays.asList(1, 2, 3);  
for (int i = 0; i < lst.size(); i++) {  
    Integer ele = lst.get(i);
    System.out.println(ele);
}
List<Integer> lst = Arrays.asList(1, 2, 3);  
for (Integer ele : lst) {  
    System.out.println(ele);
}

ArrayList sorting

  • Sort a list in ascending order
List<Integer> lst = Arrays.asList(1, 2, 3);  
lst.sort(Comparator.naturalOrder());  
  • Sort a list in descending order
List<Integer> lst = Arrays.asList(1, 2, 3);  
lst.sort(Comparator.reverseOrder());  

Conversion

  • Convert List<Integer> to int[] array
List<Integer> lst = Arrays.asList(1, 2, 3);  
int[] arr = lst.stream().mapToInt(Integer::intValue).toArray();  
  • Convert int[] array to List<Integer>
int[] arr = {1, 2, 3};  
List<Integer> lst = Arrays.stream(arr).boxed().collect(Collectors.toList());  
  • String join List<String>
List<String> lst = Arrays.asList("ABC", "ACB");  
String str = lst.stream().collect(Collectors.joining(", "));  

Notes

  • Collection.stream, Comparator.naturalOrder and Comparator.reverseOrder methods are defined in java.util package and available since Java 8