Get, Add, Remove, Retain, Sort & iterate methods of arraylist collection (java/ example)

  1. ArrayList class is resizable array implementation of the List interface.
  2. ArrayList maintains the insertion order of element or string objects.
  3. ArrayList allows the duplicate elements & we can randomly access elements by index.
  4. ArrayList is not thread safe.
    • If multiple threads access an ArrayList instance concurrently, then arraylist must be synchronized externally.
  5. Given an arraylist collection, we would demonstrate the following operations on arraylist.
    • Remove
    • Add
    • Retain
    • Sort
    • Iterate
    • Get
    • Contains And
    • Clear

1. Class Hierarchy of ArrayList Collection:

Fig 1: Class hierarchy of ArrayList collection

2. Add,sort,iterate,get,contains,clear methods of arraylist (java /example)

package org.learn.collection.list.arrayList;

import java.util.ArrayList;
import java.util.Collections;
import java.util.ListIterator;

public class DemoArrayList {

 public static void main(String[] args) {
  ArrayList<String> arrayList = new ArrayList<>();
  arrayList.add("archery");
  arrayList.add("badminton");
  arrayList.add("canoe");
  arrayList.add("boxing");
  arrayList.add("diving");
  arrayList.add("beach volleyball");

  System.out.println("1. Demo of remove methods of ArrayList: ");
  demoRemoveMethod(arrayList);

  System.out.println("\n2. Demo of add methods of ArrayList: ");
  demoAddMethod(arrayList);

  System.out.println("\n3. Demo of retain method of ArrayList: ");
  demoRetainAll(arrayList);

  System.out.println("\n4. Sort elements of arrayList collection: ");
  demoSortMethod(arrayList);

  System.out.println("\n5. Iterate through arrayList collection: ");
  demoIterateMethod(arrayList);

  System.out.println("\n6. Demo of get method of arraylist collection: ");
  System.out.println("Element at index 0: " + arrayList.get(0));
  System.out.println("Element at index 2: " + arrayList.get(2));

  System.out.println("\n7. Demo of contains method of arraylist collection: ");
  System.out.println("Check arraylist contains diving: " + arrayList.contains("diving"));
  System.out.println("Check arraylist contains soccer: " + arrayList.contains("archery"));

  System.out.println("\n8.Demo of clear method of arraylist collection: ");
  arrayList.clear();
  System.out.println("Clear arrayList: " + arrayList);
 }

 private static void demoRemoveMethod(ArrayList<String> arrayList) {
  // [archery, badminton, canoe, boxing, diving, beach volleyball]
  System.out.println("Orignal ArrayList:" + arrayList);

  // Remove element by index
  arrayList.remove(0);
  // [badminton, canoe, boxing, diving, beach volleyball]
  System.out.println("Removed element at 0 index: " + arrayList);

  // Remove element after comparing and remove it
  arrayList.remove("boxing");
  // [badminton, canoe, diving, beach volleyball]
  System.out.println("Removed boxing from arrayList: " + arrayList);

  ArrayList<String> removeElementsList = new ArrayList<>();
  removeElementsList.add("canoe");
  removeElementsList.add("diving");
  removeElementsList.add("beach volleyball");

  // Remove element by supplying another collection
  arrayList.removeAll(removeElementsList);

  // [badminton]
  System.out.println("Removed collection containing canoe and diving: " + arrayList);
 }

 private static void demoAddMethod(ArrayList<String> arrayList) {
  //[badminton]
  System.out.println("Orignal ArrayList:" + arrayList);
  arrayList.add(0, "archery");
  arrayList.add(2, "canoe");
  // [archery, badminton, canoe]
  System.out.println("Added element at 0,2 index: " + arrayList);

  arrayList.add("diving");
  // [archery, badminton, canoe, diving]
  System.out.println("Added element in list: " + arrayList);

  ArrayList<String> addElementsList = new ArrayList<>();
  addElementsList.add("squash");
  addElementsList.add("bowling");

  // It will add elements to last of list
  arrayList.addAll(addElementsList);
  // [archery, badminton, canoe, diving, squash, bowling]
  System.out.println("Added another list : " + arrayList);

  ArrayList<String> anotherList = new ArrayList<>();
  anotherList.add("golf");
  anotherList.add("judo");

  // It will add elements to last of list
  arrayList.addAll(1, anotherList);
  //[archery, golf, judo, badminton, canoe, diving, squash, bowling]
  System.out.println("Added another list at index 1 : " + arrayList);
 }

 private static void demoRetainAll(ArrayList<String> arrayList) {
  //[archery, golf, judo, badminton, canoe, diving, squash, bowling]
  System.out.println("Orignal ArrayList:" + arrayList);
  ArrayList<String> retainSportsList = new ArrayList<>();
  retainSportsList.add("soccer");
  retainSportsList.add("judo");
  retainSportsList.add("archery");
  retainSportsList.add("badminton");
  arrayList.retainAll(retainSportsList);
  // [archery, judo, badminton]
  System.out.println("Retaining elements soccer, judo and archery list: " + arrayList);
 }

 private static void demoSortMethod(ArrayList<String> arrayList) {
  // [archery, judo, badminton]
  System.out.println("Orignal ArrayList:" + arrayList);
  Collections.sort(arrayList);
  //[archery, badminton, judo]
  System.out.println("Sort ascending order: " + arrayList);

  Collections.sort(arrayList, Collections.reverseOrder());
  //[judo, badminton, archery]
  System.out.println("Sort desending order: " + arrayList);
 }

 private static void demoIterateMethod(ArrayList<String> arrayList) {

  System.out.println("Iterating using forEachRemaining:");
  // Output of for loop:
  // judo badminton archery 
  arrayList.forEach(element -> System.out.printf("%s ", element));

  System.out.println("\nIterating arrayList using foreach loop:");
  // Output of for loop:
  // judo badminton archery 
  for (String element : arrayList) {
   System.out.printf("%s ", element);
  }

  System.out.println("\nIterating arrayList using iterator:");
  ListIterator<String> listIterator = arrayList.listIterator();
  // Output of while loop:
  // judo badminton archery 
  while (listIterator.hasNext()) {
   System.out.printf("%s ", listIterator.next());
  }
  System.out.println("");
 }

}

3. Add,sort,iterate,get,contains,clear methods of arraylist (java /example)

1. Demo of remove methods of ArrayList: 
Orignal ArrayList:[archery, badminton, canoe, boxing, diving, beach volleyball]
Removed element at 0 index: [badminton, canoe, boxing, diving, beach volleyball]
Removed boxing from arrayList: [badminton, canoe, diving, beach volleyball]
Removed collection containing canoe and diving: [badminton]

2. Demo of add methods of ArrayList: 
Orignal ArrayList:[badminton]
Added element at 0,2 index: [archery, badminton, canoe]
Added element in list: [archery, badminton, canoe, diving]
Added another list : [archery, badminton, canoe, diving, squash, bowling]
Added another list at index 1 : [archery, golf, judo, badminton, canoe, diving, squash, bowling]

3. Demo of retain method of ArrayList: 
Orignal ArrayList:[archery, golf, judo, badminton, canoe, diving, squash, bowling]
Retaining elements soccer, judo and archery list: [archery, judo, badminton]

4. Sort elements of arrayList of ArrayList: 
Orignal ArrayList:[archery, judo, badminton]
Sort ascending order: [archery, badminton, judo]
Sort desending order: [judo, badminton, archery]

5. Iterate through arrayList collection: 
Iterating using forEachRemaining:
judo badminton archery 
Iterating arrayList using foreach loop:
judo badminton archery 
Iterating arrayList using iterator:
judo badminton archery 

6. Demo of get method of arraylist collection: 
Element at index 0: judo
Element at index 2: archery

7. Demo of contains method of arraylist collection: 
Check arraylist contains diving: false
Check arraylist contains soccer: true

8.Demo of clear method of arraylist collection: 
Clear arrayList: []
Scroll to Top