Traversing Techniques in ArrayList Java Collections
// Dynamic array -- ArrayList:
// 1: The size is dynamic.
// 2: It allows duplicates.
// 3: Storing null is permissible.
// 4: It enables random access to elements due to index-based storage.
// 5: ArrayList is not synchronized in Java by default.
Let's create a Java class named ArrayListConcepts with a private static method getObjects() that creates and returns an ArrayList containing an integer (10), a string ("s"), another string ("String"), a double (24.65), and a character ('g'), which is then used in the main method to demonstrate the use of ArrayList.
//Raw use of collection ArrayList:
package org.Java_Concepts.Collections;
import java.util.*;
public class ArrayListConcepts {
private static ArrayList getObjects() { //Creating Raw ArrayList
ArrayList ar = new ArrayList<>();
ar.add(10);//0
ar.add("s");
ar.add("String");
ar.add(24.65);
ar.add('g');
return ar;
}
Now, let's create a main method in the ArrayListConcepts class to show different ways to iterate over and manipulate an ArrayList in Java.
public static void main(String[] args) {
System.out.println("****dynamic array -- Arraylist:****");
System.out.println("//Raw use of collection ArrayList:");
ArrayList<Object> ar = getObjects(); //Raw use of collection ArrayList
System.out.println(ar);
System.out.println("//older for loop:");
for (int i = 0; i < ar.size(); i++) {
System.out.println(ar.get(i));
}
System.out.println("//enhance for loop:");
for (Object o : ar) {
System.out.println(o);
}
System.out.println("//Using Iterator:");
Iterator<Object> itr = ar.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
System.out.println("//Using lambda:");
ar.forEach(element -> System.out.println(element));
System.out.println("//Using Streams with lambda:");
ar.stream().forEach(element -> System.out.println(element));
System.out.println("//Using Method references: ");
ar.forEach(System.out::println);
System.out.println("//Using generic");
ArrayList<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
System.out.println(list);
The main method begins by printing a message about the dynamic array (ArrayList) and its features.
It then creates a raw ArrayList of Object type using the getObjects method, which returns an ArrayList containing various data types and is defined as a private static method within the same class.
The code then demonstrates different ways to iterate over the ArrayList: using a traditional for loop where the index is used to access each element, using an enhanced for loop for easier iteration without specifying an index, using an Iterator to traverse the ArrayList, using a lambda expression with the forEach method, using Java 8 Streams with the forEach method, and using method references with the forEach method.
Finally, the code shows how to use a generic ArrayList of String type by adding "A," "B," and "C" to the list.
This code is a comprehensive guide to understanding and working with ArrayLists in Java, demonstrating various ways to manipulate and iterate over them.
Subscribe to my newsletter
Read articles from Ashish Thakur directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by