Appearance
Introduction to Java Collections
What is a collection? A collection is an "aggregate of several defined elements." For example, a collection of five bunnies:
┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐
│ (\_(\ (\_/) (\_/) (\_/) (\(\ │
( -.-) (•.•) (>.<) (^.^) (='.')
│ C(")_(") (")_(") (")_(") (")_(") O(_")") │
└ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘
In mathematics, we often encounter the concept of a set. For example:
Finite sets:
- A collection of all the students in a class.
- A collection of all products on a website.
Infinite sets:
- The collection of all natural numbers: 1, 2, 3, …
- The collection of rational numbers.
- The collection of real numbers.
Why introduce collections in computer science? To facilitate the handling of a group of similar data, such as:
- Calculating the total and average grades of all students.
- Listing all product names and prices.
In Java, if a Java object can internally hold several other Java objects and provide access interfaces, we call this Java object a collection. Clearly, Java arrays can be seen as a type of collection:
java
String[] ss = new String[10]; // Can hold 10 String objects
ss[0] = "Hello"; // Can store a String object
String first = ss[0]; // Can retrieve a String object
Since Java provides arrays as a data type that can act as collections, why do we need other collection classes? This is because arrays have the following limitations:
- The size of an array cannot change after initialization.
- Arrays can only be accessed in index order.
Therefore, we need various types of collection classes to handle different types of data, such as:
- Resizable ordered lists.
- Collections that guarantee no duplicate elements.
Collection
The Java standard library includes the java.util
package, which provides collection classes: Collection
, the root interface for all collection classes except for Map
. The java.util
package mainly offers three types of collections:
- List: An ordered collection, for example, a list of
Student
objects arranged by index. - Set: A collection that guarantees no duplicate elements, for example, a set of
Student
objects with unique names. - Map: A collection that maps keys to values, for example, a map that finds a
Student
by name.
Java's collection design has several features: it separates interfaces from implementation classes (e.g., the ordered list interface is List
, with concrete implementations like ArrayList
, LinkedList
, etc.), and it supports generics, allowing you to restrict a collection to hold only one type of data:
java
List<String> list = new ArrayList<>(); // Can only hold String types
Finally, Java accesses collections uniformly through an iterator, which provides the significant benefit of not needing to know how the collection's internal elements are stored.
Due to the long history of Java's collection design, which has undergone significant improvements, we should note that a small number of collection classes are legacy classes and should not be used anymore:
- Hashtable: A thread-safe implementation of
Map
. - Vector: A thread-safe implementation of
List
. - Stack: A LIFO stack based on
Vector
.
Additionally, a small number of interfaces are legacy interfaces and should also be avoided:
Enumeration<E>
: Replaced byIterator<E>
.
Summary
Java's collection classes are defined in the java.util
package, support generics, and primarily offer three types of collections: List
, Set
, and Map
. Java collections use a uniform Iterator
for traversal, and legacy interfaces should be avoided whenever possible.