Core Collection Interfaces 

  1. General Rules and Definitions
  2. The Collection Interface Hierarchy
  3. Examples

General Rules and Definitions


The Collection Interface Hierarchy


 
Collection
  • A group of objects with no particular restrictions (e.g., a Bag)
  • Two constructors should be provided:
    • Void (no argument) constructor - create an empty Collection
    • Collection as argument - create a Collection containing same elements as constructor argument
Set
  • Extends Collection, but adds no methods
  • No duplicates permitted - implied contract is that a constructor may not create a set with duplicates
SortedSet
  • Extends Set
  • Elements sorted in some order - elements must implement Comparable, or SortedSet must have a Comparator associated with it
  • Iterator will traverse elements in ascending order
  • Four constructors should be provided:
    • Void (no argument) constructor - create an empty SortedSet
    • Collection as argument
      • create a SortedSet containing same elements as constructor argument
      • ordering follows elements natural order
    • Comparator as argument
      • create an empty SortedSet
      • ordering is managed by Comparator
    • SortedSet as argument - create a SortedSet with same elements as argument.
List
  • Extends Collection
  • Also known as a sequence
  • Ordered
    • Provides positional access/insertions
    • Position specified by integer
  • Duplicates permitted
  • Similar to JDK 1.1 Vector, but unsynchronized
  • ListIterator functionality
Map
  • Mapping of one key to one value:
    • No duplicate keys permitted
    • Each key can map to at most one value
  • Ordering determined by order of elements returned by Iterator
  • Provides 3 Collection views for interoperability
  • Similar to JDK 1.1 Hashtable, but unsynchronized
SortedMap
  • Extends Map
  • Elements sorted in some order (by key)
    • Iterator on any of the 3 views will traverse in ascending key order
    • Keys must implement Comparable, or SortedMap must have a Comparator associated with it
  • Four constructors should be provided:
    • Void (no argument) constructor - create an empty SortedMap
    • Collection as argument
      • create a SortedMap containing same elements as constructor argument
      • ordering follows elements natural order
    • Comparator as argument
      • create an empty SortedMap
      • ordering is managed by Comparator
    • SortedMap as argument - create a SortedMap with same elements as argument.
 

Examples