Java Data Structures

Arrays, ArrayLists and HashMaps

Chuck Nelson

Introduction to Data Structures

This presentation will cover three fundamental data structures in Java:

  • Arrays
  • ArrayLists
  • HashMaps

We will explore their characteristics, how to use them, and when to choose one over the other.

Arrays

Arrays are fixed-size, ordered collections of elements of the same type.

  • Size: The size of an array is determined at creation and cannot be changed.
  • Type: All elements must be of the same data type (e.g., int, String, Object).
  • Indexing: Elements are accessed via a zero-based index.

Arrays: Operations

// Create an array of strings with a size of 5
String[] fruits = new String[5];

// Add elements
fruits[0] = "Apple";
fruits[1] = "Banana";
fruits[2] = "Orange";

// Access an element
String fruit = fruits[1]; // "Banana"

// Remove an element (by setting it to null)
fruits[1] = null; 

// Get the size
int size = fruits.length; // 5

Arrays: Use Cases

  • When you know the exact number of elements you need to store.
  • For performance-critical operations where the overhead of a dynamic data structure is not acceptable.
  • Storing and processing large amounts of primitive data.

ArrayLists

ArrayList is a dynamic, ordered collection of elements. It is part of the Java Collections Framework.

  • Size: The size of an ArrayList can grow or shrink dynamically.
  • Type: Can store objects of any type.
  • Indexing: Elements are accessed via a zero-based index.

Important

The ArrayList class does not support primitive types directly. To work with primitives like int, you should use wrapper classes (Integer) instead.

ArrayLists: Operations

import java.util.ArrayList;

// Create an ArrayList of strings
ArrayList<String> vegetables = new ArrayList<>();

// Add elements
vegetables.add("Carrot");
vegetables.add("Broccoli");
vegetables.add("Spinach");

// Access an element
String vegetable = vegetables.get(1); // "Broccoli"

// Remove an element
vegetables.remove(1); // Removes "Broccoli"

// Get the size
int size = vegetables.size(); // 2

ArrayLists: Use Cases

  • When you don’t know the number of elements you need to store.
  • When you need to frequently add or remove elements from the collection.
  • For general-purpose object storage.

HashMaps

HashMap is a collection of key-value pairs. It is part of the Java Collections Framework.

  • Size: The size of a HashMap can grow or shrink dynamically.
  • Type: Can store objects of any type for both keys and values.
  • Indexing: Elements are accessed via their key, not an index.

Important

The HashMap class does not support primitive types directly. To work with primitives like int, you should use wrapper classes (Integer) instead.

HashMaps: Operations

import java.util.HashMap;

// Create a HashMap with String keys and Integer values
HashMap<String, Integer> ages = new HashMap<>();

// Add elements
ages.put("Alice", 30);
ages.put("Bob", 25);
ages.put("Charlie", 35);

// Access an element
int age = ages.get("Bob"); // 25

// Remove an element
ages.remove("Charlie");

// Get the size
int size = ages.size(); // 2

HashMaps: Use Cases

  • When you need to store and retrieve data as key-value pairs.
  • For fast lookups, insertions, and deletions based on a key.
  • Implementing caches, symbol tables, and other lookup-based data structures.

Comparison

Feature Array ArrayList HashMap
Size Fixed Dynamic Dynamic
Ordering Ordered Ordered Unordered
Indexing Integer index (0-based) Integer index (0-based) Key (Object)
Storage Primitives and Objects Objects only Objects only (Key-Value pairs)
Performance Fast for access, slow for resizing Slower access, flexible resizing Fast for key-based access

Conclusion

  • Arrays: Use for fixed-size collections, especially with primitive types, for maximum performance.
  • ArrayLists: The go-to choice for dynamic, ordered collections of objects.
  • HashMaps: Ideal for fast lookups and when data is naturally represented as key-value pairs.

Choosing the right data structure is crucial for writing efficient and maintainable Java code.