In common usage, a collection is the same as the intuitive, mathematical concept of a set. A set is just a group of unique items, meaning that the group contains no duplicates. The Collections Framework, in fact, includes a Set
interface, and a number of concrete Set
classes. But the formal notion of a set predates Java technology by a century, when the British mathematician George Boole defined it in formal logic. Most people learned some set theory in elementary school when introduced to "set intersection" and "set union" through the familiar Venn Diagrams:
Some real-world examples of sets include the following:
- The set of uppercase letters 'A' through 'Z'
- The set of non-negative integers {0, 1, 2 ...}
- The set of reserved Java programming language
keywords {'import', 'class', 'public', 'protected'...}
- A set of people (friends, employees, clients, ...)
- The set of records returned by a database query
- The set of
Component
objects in a Container
- The set of all pairs
- The empty set {}
Sets have the following basic properties:
- They contains only one instance of each item
- They may be finite or infinite
- They can define abstract concepts
Sets are fundamental to logic, mathematics, and computer
science, but also practical in everyday applications in business
and systems. The idea of a "connection pool" is a set
of open connections to a database server. Web servers have to
manage sets of clients and connections. File descriptors provide
another example of a set in the operating system.
A map is a special kind of set. It is a set of pairs, each pair representing a one-directional mapping from one element to another. Some examples of maps are:
- The map of IP addresses to domain names (DNS)
- A map from keys to database records
- A dictionary (words mapped to meanings)
- The conversion from base 2 to base 10
Like sets, the idea behind a map is much older than the Java
programming language, older even than computer science. Sets and
maps are important tools in mathematics and their properties are
well understood. People also long recognized the usefulness of
solving programming problems with sets and maps. A language
called SETL (Set Language) invented in 1969 included sets as one
of its only primitive data types (SETL also included garbage
collection -- not widely accepted until Java technology was developed
in the 1990s). Although sets and maps appear in many languages
including C++, the Collections Framework is perhaps the best
designed set and map package yet written for a popular language.
(Users of C++ Standard Template Library (STL) and Smalltalk's
collection hierarchy might argue that last point.)
Also because they are sets, maps can be finite or
infinite. An example of an infinite map is the conversion from base 2 to base
10. Unfortunately, the Collections Framework does not support infinite
maps -- sometimes a mathematical function, formula, or algorithm is
preferred. But when a problem can be solved with a finite map,
the Collections Framework provides the Java programmer with a
useful API.
Because the Collections Framework has formal definitions for
the classes Set
, Collection
, and Map
,
you'll notice the lower case words set, collection,
and map to distinguish the implementation from
the concept.