Java

Arrays and Collections

Arrays and Collections in Java: Organizing Multiple Values

In programming, you often need to store and work with multiple values together. Java provides powerful tools for this: arrays and collections. Understanding these will help you manage data efficiently.

This guide covers:

  • What arrays are and how to use them

  • Introduction to Java Collections Framework

  • Common collection types and when to use them


Arrays: Fixed-Size Containers for Data

An array is a container that holds a fixed number of values of the same type.

Declaring and Initializing Arrays

java
int[] numbers = new int[5]; // array of 5 integers, default 0
String[] fruits = {"Apple", "Banana", "Cherry"}; // initialized array

Accessing Array Elements

Array indices start at 0:

java
System.out.println(fruits[0]); // Output: Apple
fruits[1] = "Blueberry"; // Change second element

Looping Through Arrays

java
for (int i = 0; i < fruits.length; i++) {
System.out.println(fruits[i]);
}

Or using enhanced for-loop:

java
for (String fruit : fruits) {
System.out.println(fruit);
}

️ Collections: Flexible Data Structures

While arrays have a fixed size, collections provide flexible and powerful ways to store groups of objects.

Java’s Collections Framework offers interfaces and classes like:

Collection Type Description Common Classes
List Ordered collection, allows duplicates ArrayList, LinkedList
Set Unordered collection, no duplicates HashSet, TreeSet
Map Key-value pairs HashMap, TreeMap
Queue FIFO collection LinkedList, PriorityQueue

Common Collections and Usage

1. ArrayList (Resizable Array)

java
import java.util.ArrayList;

ArrayList<String> colors = new ArrayList<>();
colors.add("Red");
colors.add("Green");
colors.add("Blue");

for (String color : colors) {
System.out.println(color);
}

2. HashSet (Unique Elements)

java
import java.util.HashSet;

HashSet<String> uniqueNames = new HashSet<>();
uniqueNames.add("Alice");
uniqueNames.add("Bob");
uniqueNames.add("Alice"); // Duplicate ignored

System.out.println(uniqueNames.size()); // Output: 2

3. HashMap (Key-Value Storage)

java
import java.util.HashMap;

HashMap<String, Integer> ages = new HashMap<>();
ages.put("Alice", 30);
ages.put("Bob", 25);

System.out.println("Alice is " + ages.get("Alice") + " years old.");


⚡ When to Use Arrays vs Collections?

Feature Arrays Collections
Size Fixed at creation Dynamic size
Type safety Store primitives and objects Store only objects (use wrappers for primitives)
Flexibility Less flexible More flexible with many utilities
Performance Faster for fixed-size, primitive data Slightly slower due to overhead

Example: Using ArrayList to Store and Print Names

java
import java.util.ArrayList;

public class Main {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
names.add("Charlie");

for (String name : names) {
System.out.println(name);
}
}
}


Summary

  • Arrays hold fixed-size, same-type elements

  • Collections offer dynamic, flexible data structures

  • Use Lists for ordered collections, Sets for uniqueness, Maps for key-value pairs

  • Choose arrays for simple fixed datasets, collections for more complex needs

Leave a Reply

Your email address will not be published. Required fields are marked *