Unordered Collections

last updated 4/27/07

Introduction

Collections used so far are ordered.  There is a predecessor or successor or parent

Sometimes these structures are used for unordered data items.

This chapter is concerned with collections that do not have order as part of the structure. 

 

 

 


Sets

This is conceptually the same as the mathematical set.

The expected operations would be

  • boolean isEmpty()
  • int size()
  • boolean add(Object)
  • boolean remove(Object)
  • boolean contains(Object)
  • Set union
  • Set intersection
  • Set difference
  • boolean subset

A = {a, b, e, f, h} and B = {a, c, f, k}

 


Maps

A map is a collection of key-value pairs.  The collection is sometimes called a Dictionary.

This has the characteristic of the mathematical function.  The keys are the domain and values are the range.

Operations

 


Bags

A bag is a set that allow duplicate items.  If an item has two occurrences, then it can be removed twice as well.

There would be an internal counter maintaining a frequency count.

The operations are the same as a set except no union, intersection or difference operations.

 

 

 

 


Implementation

These unordered collections need a way to quickly store an object and quickly determine whether it exists in the collection or not and retrieve it.  In the case of the map, retrieve the value.

An array or linked list forces the processing to a sequential nature --> O(n).

We need fast storage and fast retrieval.  Ideally we want O(1) performance.

With hashing, we can approach this level of performance.