Skip to main content

ArrayLists and the Collections Framework in Java

Introduction​

Java's Collections Framework provides a set of classes and interfaces for storing and manipulating groups of data as a single unit. ArrayList is one of the most commonly used classes in this framework, offering dynamic arrays that can grow and shrink in size.

ArrayList​

An ArrayList is a resizable array that provides more functionality and flexibility compared to a standard array. It is part of the java.util package.

Declaration​

To use ArrayList, you need to import it from the java.util package.

Syntax​

import java.util.ArrayList;

ArrayList<dataType> arrayListName = new ArrayList<>();

Example​

import java.util.ArrayList;

public class Main {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();

// Adding elements
names.add("Alice");
names.add("Bob");
names.add("Charlie");

// Accessing elements
System.out.println(names.get(0)); // Outputs: Alice

// Modifying elements
names.set(1, "Robert");

// Removing elements
names.remove(2);

// Iterating over the ArrayList
for (String name : names) {
System.out.println(name);
}
}
}

Common Methods​

  • add(element): Adds an element to the end of the list.
  • add(index, element): Inserts an element at the specified index.
  • get(index): Returns the element at the specified index.
  • set(index, element): Replaces the element at the specified index with the specified element.
  • remove(index): Removes the element at the specified index.
  • remove(element): Removes the first occurrence of the specified element.
  • size(): Returns the number of elements in the list.
  • clear(): Removes all elements from the list.
  • isEmpty(): Returns true if the list contains no elements.

Collections Framework​

The Collections Framework provides a unified architecture for representing and manipulating collections. It includes interfaces, implementations, and algorithms.

Interfaces​

  • Collection: The root interface for all collections.
  • List: An ordered collection (also known as a sequence). ArrayList and LinkedList are its implementations.
  • Set: A collection that contains no duplicate elements. HashSet and TreeSet are its implementations.
  • Queue: A collection used to hold multiple elements prior to processing. LinkedList and PriorityQueue are its implementations.
  • Map: An object that maps keys to values. HashMap and TreeMap are its implementations.

Example: Using Different Collections​

import java.util.*;

public class Main {
public static void main(String[] args) {
// List example
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Orange");
System.out.println("List: " + list);

// Set example
Set<String> set = new HashSet<>();
set.add("Apple");
set.add("Banana");
set.add("Apple"); // Duplicate element will not be added
System.out.println("Set: " + set);

// Queue example
Queue<String> queue = new LinkedList<>();
queue.add("Apple");
queue.add("Banana");
queue.add("Orange");
System.out.println("Queue: " + queue);
System.out.println("Queue poll: " + queue.poll()); // Removes and returns the head of the queue

// Map example
Map<String, Integer> map = new HashMap<>();
map.put("Apple", 1);
map.put("Banana", 2);
map.put("Orange", 3);
System.out.println("Map: " + map);
System.out.println("Map get: " + map.get("Banana")); // Returns the value for the specified key
}
}

Algorithms​

The Collections class provides static methods that operate on collections, such as sorting and searching.

Example: Sorting a List​

import java.util.*;

public class Main {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Banana");
list.add("Apple");
list.add("Orange");

Collections.sort(list); // Sorts the list in natural order
System.out.println("Sorted List: " + list);

Collections.sort(list, Collections.reverseOrder()); // Sorts the list in reverse order
System.out.println("Reverse Sorted List: " + list);
}
}

Conclusion​

The Collections Framework in Java provides a powerful and flexible set of classes and interfaces for managing groups of objects. ArrayList is a versatile and commonly used class within this framework. Understanding how to use ArrayList and other collections, as well as the algorithms provided by the Collections class, is crucial for effective Java programming.