CS61B Guide
Search…
Collections
An overview of all the Collections in Java.
Collection is a Java interface for common abstract data types that store multiple items in them.

Sub-Interfaces

  • Lists are indexed sequences with duplication. The two most common types are ArrayLists and Linked Lists.
  • Sets are non-indexed sequences with no duplication. (That is, every value in a set is unique.)
  • Maps are key-value pairs. See Hashing and Hash Tables for a description on one common map implementation, the HashMap. All keys in a map must be unique, but values can be duplicated.
  • Stacks and Queues are two ordered collections that have two core behaviors:
    • push(T x): puts x on the top.
    • pop(): Removes the first item. (See the stacks and queues page for more information.)

Common Functions

  • Membership tests contains() and containsAll() that can determine whether or not an element is in the collection.
  • size() to get the number of items in the collection.
  • isEmpty() returns true if there is nothing in the collection.
  • iterator() returns an Iterator object to go through all the values in the collection.
  • toArray() converts the collection to a standard Java array.
  • Optional functions that aren't implemented in the interface: add, addAll, clear, remove, removeAll, retainAll (intersection)
    • Throws UnsupportedOperationException if not implemented.