Skip to main content

Working with Arrays and Collections in Java

Introduction​

Arrays and collections are essential data structures in Java that allow you to store and manipulate groups of elements. This guide covers common operations and techniques for working with both arrays and collections.

Arrays​

Declaration and Initialization​

Single-Dimensional Arrays​

// Declaration and initialization
int[] numbers = {1, 2, 3, 4, 5};
String[] names = {"Alice", "Bob", "Charlie"};

Multi-Dimensional Arrays​

// Declaration and initialization
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

Accessing and Modifying Elements​

// Accessing elements
int firstNumber = numbers[0];
int matrixElement = matrix[1][2];

// Modifying elements
numbers[1] = 10;
matrix[0][0] = 99;

Iterating Over Arrays​

// Using for loop
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}

// Using enhanced for loop
for (int number : numbers) {
System.out.println(number);
}

Collections​

List Interface​

ArrayList​

import java.util.ArrayList;

// Declaration and initialization
ArrayList<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
names.add("Charlie");

// Accessing and modifying elements
String firstElement = names.get(0);
names.set(1, "Robert");

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

Set Interface​

HashSet​

import java.util.HashSet;

// Declaration and initialization
HashSet<String> set = new HashSet<>();
set.add("Apple");
set.add("Banana");
set.add("Apple"); // Duplicate element will not be added

// Iterating over the HashSet
for (String element : set) {
System.out.println(element);
}

Map Interface​

HashMap​

import java.util.HashMap;

// Declaration and initialization
HashMap<String, Integer> map = new HashMap<>();
map.put("Apple", 1);
map.put("Banana", 2);
map.put("Orange", 3);

// Accessing elements
int value = map.get("Banana");

// Iterating over the HashMap
for (String key : map.keySet()) {
System.out.println(key + ": " + map.get(key));
}

Queue Interface​

LinkedList​

import java.util.LinkedList;
import java.util.Queue;

// Declaration and initialization
Queue<String> queue = new LinkedList<>();
queue.add("Apple");
queue.add("Banana");
queue.add("Orange");

// Accessing and removing elements
String head = queue.poll();

// Iterating over the Queue
for (String element : queue) {
System.out.println(element);
}

Conclusion​

Working with arrays and collections in Java allows you to efficiently manage and manipulate groups of data. Whether you're using arrays for fixed-size collections or collections for dynamic data handling, mastering these data structures is essential for effective Java programming.