Java Collections Framework is a set of interfaces and classes that provides an architecture to store and manipulate groups of objects. It's one of the most powerful features of Java, allowing for efficient data management and manipulation.

Overview

The Java Collections Framework provides a unified architecture for representing and manipulating collections of objects. It includes a range of interfaces and classes, each serving different purposes. The most commonly used interfaces are:

  • List: An ordered collection of objects.
  • Set: A collection of objects that cannot contain duplicate elements.
  • Queue: A collection used to hold elements prior to processing.
  • Map: A collection used to store key-value pairs.

Key Components

Interfaces

  • Collection: The root interface of the collection hierarchy. It provides the basic methods for manipulating collections.
  • List, Set, Queue, Deque, Stack, Map, SortedSet, SortedMap: Specific interfaces for different types of collections.

Classes

  • ArrayList, LinkedList, Vector, Stack: Implementations of the List interface.
  • HashSet, LinkedHashSet, TreeSet: Implementations of the Set interface.
  • PriorityQueue, ArrayDeque: Implementations of the Queue interface.
  • HashMap, LinkedHashMap, TreeMap, IdentityHashMap, EnumMap: Implementations of the Map interface.

Example Usage

Here's an example of how you can use the ArrayList class to store and manipulate a list of strings:

import java.util.ArrayList;

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

        System.out.println("First element: " + list.get(0));
        System.out.println("Last element: " + list.get(list.size() - 1));
        System.out.println("List contains 'Banana': " + list.contains("Banana"));
    }
}

Learn More

To learn more about the Java Collections Framework, check out our in-depth guide.


Images

  • ArrayList
  • LinkedList
  • HashSet
  • HashMap