CISC 3130
Data Structures
Containers I
Introduction to Containers — ADT's; Motivation & Examples

Overview

A container/collection is a data type whose primary purpose is to contain a group of objects.

ADT's Revisited

The Containers/Collections

We'll start off with a class analogous to the built-in array as its operations, and semantics are familiar (we did the same thing back in Lecture 1, where we originally introduced this class). We do this to illustrate how we will be presenting the rest of the containers. In each case, we will have:

Array

A 1-dimensional collection of elements randomly accessed by their index/position/offset from the beginning of the array.

Overview

Operations


Interface

interface Array<E> {
	E get(int index);
	void set(int index, E value);
	int length();
}

Motivation and Examples

Demo Code and Output

I'm showing this demo because there's no real Array contain in the JCF so there's nothing for you to play with
class ArrayDemo {
	public static void main(String [] args) {
		System.out.println("========== Array ==========");
		Array<Integer> array = new Array<>();

		System.out.println("length: " + array.length());
		System.out.println();

		System.out.println("===== Setting (then Printing) =====");
		for (int i = 0; i < array.length(); i++)
			array.set(i, i * 10);
		System.out.println(array);

		System.out.println();
		System.out.println("===== Getting =====");
		for (int i = 0; i < array.length(); i++)
			System.out.print(array.get(i) + (i < array.length() - 1 ? ", " : ""));
		System.out.println();
	}
}
Notes
Each of the containers is presented in the context of a demo app that 'shows off' the capabilities of the container.

What the demo is doing:

========== Array ==========
length: 100

===== Setting then Printing =====
{0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250, 260, \
	270, 280, 290, 300, 310, 320, 330, 340, 350, 360, 370, 380, 390, 400, 410, 420, 430, 440, 450, 460, 470, 480, 490, 500, \
	510, 520, 530, 540, 550, 560, 570, 580, 590, 600, 610, 620, 630, 640, 650, 660, 670, 680, 690, 700, 710, 720, 730, 740, \
	750, 760, 770, 780, 790, 800, 810, 820, 830, 840, 850, 860, 870, 880, 890, 900, 910, 920, 930, 940, 950, 960, 970, 980, 990}

===== Getting =====
0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250, 260, \
	270, 280, 290, 300, 310, 320, 330, 340, 350, 360, 370, 380, 390, 400, 410, 420, 430, 440, 450, 460, 470, 480, 490, 500, \
	510, 520, 530, 540, 550, 560, 570, 580, 590, 600, 610, 620, 630, 640, 650, 660, 670, 680, 690, 700, 710, 720, 730, 740, \ 
	750, 760, 770, 780, 790, 800, 810, 820, 830, 840, 850, 860, 870, 880, 890, 900, 910, 920, 930, 940, 950, 960, 970, 980, 990
Notes


Sequence (position-accessed)

Sequences are ordered lists of elements that have a position within the sequence. The element are accessed strictly by that position

Stack

A stack exhibits LIFO (lat-in-first-out) storage retrieval. It is a container in which insertion/removal is done from one end of the container.

Operations

interface Stack<E> {
	void push(T value)
	T pop()
	boolean isEmpty()
}

Motivation and Examples

Demo Output

========== Stack ==========
{} (true)

Pushing
-------
0 -> {0} (false)
10 -> {0, 10} (false)
20 -> {0, 10, 20} (false)
30 -> {0, 10, 20, 30} (false)
40 -> {0, 10, 20, 30, 40} (false)
50 -> {0, 10, 20, 30, 40, 50} (false)
60 -> {0, 10, 20, 30, 40, 50, 60} (false)
70 -> {0, 10, 20, 30, 40, 50, 60, 70} (false)
80 -> {0, 10, 20, 30, 40, 50, 60, 70, 80} (false)
90 -> {0, 10, 20, 30, 40, 50, 60, 70, 80, 90} (false)

Popping
-------
{0, 10, 20, 30, 40, 50, 60, 70, 80, 90} (false) -> 90
{0, 10, 20, 30, 40, 50, 60, 70, 80} (false) -> 80
{0, 10, 20, 30, 40, 50, 60, 70} (false) -> 70
{0, 10, 20, 30, 40, 50, 60} (false) -> 60
{0, 10, 20, 30, 40, 50} (false) -> 50
{0, 10, 20, 30, 40} (false) -> 40
{0, 10, 20, 30} (false) -> 30
{0, 10, 20} (false) -> 20
{0, 10} (false) -> 10
{0} (false) -> 0
{} (true)
Notes What the demo is doing:


Queue (Single-ended)

A data type typically used for some form of scheduling. In the simplest case (the FIFO queue below), items are inserted at one end (the back or rear) and removed from the other (front or head)

FIFO Queue

Overview

A (fifo) queue exhibits FIFO (first-in-first-out) storage retrieval. It is a container in which elements are added at one end and removed from the other.

Operations

<

Interface

interface Queue<E> {
	void add(T value)
	T remove()
	boolean isEmpty()
}
Notes
Motivation and Examples

Demo Output

========== FifoQueue ==========
{} (true)

Adding
------
0 -> {0} (false)
10 -> {0, 10} (false)
20 -> {0, 10, 20} (false)
30 -> {0, 10, 20, 30} (false)
40 -> {0, 10, 20, 30, 40} (false)
50 -> {0, 10, 20, 30, 40, 50} (false)
60 -> {0, 10, 20, 30, 40, 50, 60} (false)
70 -> {0, 10, 20, 30, 40, 50, 60, 70} (false)
80 -> {0, 10, 20, 30, 40, 50, 60, 70, 80} (false)
90 -> {0, 10, 20, 30, 40, 50, 60, 70, 80, 90} (false)

Removing
--------
{0, 10, 20, 30, 40, 50, 60, 70, 80, 90} (false) -> 0
{10, 20, 30, 40, 50, 60, 70, 80, 90} (false) -> 10
{20, 30, 40, 50, 60, 70, 80, 90} (false) -> 20
{30, 40, 50, 60, 70, 80, 90} (false) -> 30
{40, 50, 60, 70, 80, 90} (false) -> 40
{50, 60, 70, 80, 90} (false) -> 50
{60, 70, 80, 90} (false) -> 60
{70, 80, 90} (false) -> 70
{80, 90} (false) -> 80
{90} (false) -> 90
{} (true)
Notes
What the demo is doing:



Priority Queue

A priority queue removes the largest (smallest) element in the queue. The challenge is to do this in an efficient manner. We'll investigate that later.

Operations

, or priority-baseD
interface PriorityQueue<E> {
	void add(T value)
	T remove()
	boolean isEmpty()
}

Motivation and Examples

Demo Code and Output

========== PriorityQueue ==========
{} (true)

Adding
------
251 -> {251} (false)
80 -> {251, 80} (false)
241 -> {251, 80, 241} (false)
828 -> {251, 80, 241, 828} (false)
55 -> {251, 80, 241, 828, 55} (false)
84 -> {251, 80, 241, 828, 55, 84} (false)
375 -> {251, 80, 241, 828, 55, 84, 375} (false)
802 -> {251, 80, 241, 828, 55, 84, 375, 802} (false)
501 -> {251, 80, 241, 828, 55, 84, 375, 802, 501} (false)
389 -> {251, 80, 241, 828, 55, 84, 375, 802, 501, 389} (false)

Removing
--------
{251, 80, 241, 828, 55, 84, 375, 802, 501, 389} (false) -> 828
{251, 80, 241, 55, 84, 375, 802, 501, 389} (false) -> 802
{251, 80, 241, 55, 84, 375, 501, 389} (false) -> 501
{251, 80, 241, 55, 84, 375, 389} (false) -> 389
{251, 80, 241, 55, 84, 375} (false) -> 375
{251, 80, 241, 55, 84} (false) -> 251
{80, 241, 55, 84} (false) -> 241
{80, 55, 84} (false) -> 84
{80, 55} (false) -> 80
{55} (false) -> 55
{} (true)
Notes
What the demo is doing:



Deque (double-ended queue)

A double-ended queue (deque) provides both LIFO and FIFO storage retrieval (as well as a mixture). It is a container that permits insrtion/removal from either end.

Operations

Interface

interface Deque<E> {
	void addFirst(E value)
	void addLast(E value)
	E removeFirst()
	E removeLast()
	boolean isEmpty()
}

Motivation and Examples

Demo Code and Output

========== Deque ==========
{} (true)

Adding First and Last Alternately
---------------------------------
0 -> {0} (false)
0 -> {0, 0} (false)
10 -> {10, 0, 0} (false)
10 -> {10, 0, 0, 10} (false)
20 -> {20, 10, 0, 0, 10} (false)
20 -> {20, 10, 0, 0, 10, 20} (false)
30 -> {30, 20, 10, 0, 0, 10, 20} (false)
30 -> {30, 20, 10, 0, 0, 10, 20, 30} (false)
40 -> {40, 30, 20, 10, 0, 0, 10, 20, 30} (false)
40 -> {40, 30, 20, 10, 0, 0, 10, 20, 30, 40} (false)
50 -> {50, 40, 30, 20, 10, 0, 0, 10, 20, 30, 40} (false)
50 -> {50, 40, 30, 20, 10, 0, 0, 10, 20, 30, 40, 50} (false)
60 -> {60, 50, 40, 30, 20, 10, 0, 0, 10, 20, 30, 40, 50} (false)
60 -> {60, 50, 40, 30, 20, 10, 0, 0, 10, 20, 30, 40, 50, 60} (false)
70 -> {70, 60, 50, 40, 30, 20, 10, 0, 0, 10, 20, 30, 40, 50, 60} (false)
70 -> {70, 60, 50, 40, 30, 20, 10, 0, 0, 10, 20, 30, 40, 50, 60, 70} (false)
80 -> {80, 70, 60, 50, 40, 30, 20, 10, 0, 0, 10, 20, 30, 40, 50, 60, 70} (false)
80 -> {80, 70, 60, 50, 40, 30, 20, 10, 0, 0, 10, 20, 30, 40, 50, 60, 70, 80} (false)
90 -> {90, 80, 70, 60, 50, 40, 30, 20, 10, 0, 0, 10, 20, 30, 40, 50, 60, 70, 80} (false)
90 -> {90, 80, 70, 60, 50, 40, 30, 20, 10, 0, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90} (false)

Removing First and Last Alternately
-----------------------------------
{90, 80, 70, 60, 50, 40, 30, 20, 10, 0, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90} (false) -> 90
{80, 70, 60, 50, 40, 30, 20, 10, 0, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90} (false) -> 90
{80, 70, 60, 50, 40, 30, 20, 10, 0, 0, 10, 20, 30, 40, 50, 60, 70, 80} (false) -> 80
{70, 60, 50, 40, 30, 20, 10, 0, 0, 10, 20, 30, 40, 50, 60, 70, 80} (false) -> 80
{70, 60, 50, 40, 30, 20, 10, 0, 0, 10, 20, 30, 40, 50, 60, 70} (false) -> 70
{60, 50, 40, 30, 20, 10, 0, 0, 10, 20, 30, 40, 50, 60, 70} (false) -> 70
{60, 50, 40, 30, 20, 10, 0, 0, 10, 20, 30, 40, 50, 60} (false) -> 60
{50, 40, 30, 20, 10, 0, 0, 10, 20, 30, 40, 50, 60} (false) -> 60
{50, 40, 30, 20, 10, 0, 0, 10, 20, 30, 40, 50} (false) -> 50
{40, 30, 20, 10, 0, 0, 10, 20, 30, 40, 50} (false) -> 50
{40, 30, 20, 10, 0, 0, 10, 20, 30, 40} (false) -> 40
{30, 20, 10, 0, 0, 10, 20, 30, 40} (false) -> 40
{30, 20, 10, 0, 0, 10, 20, 30} (false) -> 30
{20, 10, 0, 0, 10, 20, 30} (false) -> 30
{20, 10, 0, 0, 10, 20} (false) -> 20
{10, 0, 0, 10, 20} (false) -> 20
{10, 0, 0, 10} (false) -> 10
{0, 0, 10} (false) -> 10
{0, 0} (false) -> 0
{0} (false) -> 0
{} (true)
Notes
What the demo is doing:

List

Same as sequence

Overview

Operations

Interface

interface List<E> {
	E get(int index);
	void set(int index, E value);
	void add(int index, E value);
	E remove(int index);
	int size();
}

Motivation and Examples

Demo Code and Output

Notes
What the demo is doing:

========== List ==========
size: 0
{} (true)

Appending (adding at end - value @ index)
-----------------------------------------
0 @ 0 -> {0} (false)
10 @ 1 -> {0, 10} (false)
20 @ 2 -> {0, 10, 20} (false)
30 @ 3 -> {0, 10, 20, 30} (false)
40 @ 4 -> {0, 10, 20, 30, 40} (false)
50 @ 5 -> {0, 10, 20, 30, 40, 50} (false)
60 @ 6 -> {0, 10, 20, 30, 40, 50, 60} (false)
70 @ 7 -> {0, 10, 20, 30, 40, 50, 60, 70} (false)
80 @ 8 -> {0, 10, 20, 30, 40, 50, 60, 70, 80} (false)
90 @ 9 -> {0, 10, 20, 30, 40, 50, 60, 70, 80, 90} (false)

Getting
-------
0, 10, 20, 30, 40, 50, 60, 70, 80, 90

Inserting within the List (value @ index)
-----------------------------------------
9000 @ 9 -> {0, 10, 20, 30, 40, 50, 60, 70, 80, 9000, 90} (false)
8000 @ 8 -> {0, 10, 20, 30, 40, 50, 60, 70, 8000, 80, 9000, 90} (false)
7000 @ 7 -> {0, 10, 20, 30, 40, 50, 60, 7000, 70, 8000, 80, 9000, 90} (false)
6000 @ 6 -> {0, 10, 20, 30, 40, 50, 6000, 60, 7000, 70, 8000, 80, 9000, 90} (false)
5000 @ 5 -> {0, 10, 20, 30, 40, 5000, 50, 6000, 60, 7000, 70, 8000, 80, 9000, 90} (false)
4000 @ 4 -> {0, 10, 20, 30, 4000, 40, 5000, 50, 6000, 60, 7000, 70, 8000, 80, 9000, 90} (false)
3000 @ 3 -> {0, 10, 20, 3000, 30, 4000, 40, 5000, 50, 6000, 60, 7000, 70, 8000, 80, 9000, 90} (false)
2000 @ 2 -> {0, 10, 2000, 20, 3000, 30, 4000, 40, 5000, 50, 6000, 60, 7000, 70, 8000, 80, 9000, 90} (false)
1000 @ 1 -> {0, 1000, 10, 2000, 20, 3000, 30, 4000, 40, 5000, 50, 6000, 60, 7000, 70, 8000, 80, 9000, 90} (false)
0 @ 0 -> {0, 0, 1000, 10, 2000, 20, 3000, 30, 4000, 40, 5000, 50, 6000, 60, 7000, 70, 8000, 80, 9000, 90} (false)

Removing elements < 1000
------------------------
{0, 0, 1000, 10, 2000, 20, 3000, 30, 4000, 40, 5000, 50, 6000, 60, 7000, 70, 8000, 80, 9000, 90} (false) -> 90 @ 19
{0, 0, 1000, 10, 2000, 20, 3000, 30, 4000, 40, 5000, 50, 6000, 60, 7000, 70, 8000, 80, 9000} (false) -> 80 @ 17
{0, 0, 1000, 10, 2000, 20, 3000, 30, 4000, 40, 5000, 50, 6000, 60, 7000, 70, 8000, 9000} (false) -> 70 @ 15
{0, 0, 1000, 10, 2000, 20, 3000, 30, 4000, 40, 5000, 50, 6000, 60, 7000, 8000, 9000} (false) -> 60 @ 13
{0, 0, 1000, 10, 2000, 20, 3000, 30, 4000, 40, 5000, 50, 6000, 7000, 8000, 9000} (false) -> 50 @ 11
{0, 0, 1000, 10, 2000, 20, 3000, 30, 4000, 40, 5000, 6000, 7000, 8000, 9000} (false) -> 40 @ 9
{0, 0, 1000, 10, 2000, 20, 3000, 30, 4000, 5000, 6000, 7000, 8000, 9000} (false) -> 30 @ 7
{0, 0, 1000, 10, 2000, 20, 3000, 4000, 5000, 6000, 7000, 8000, 9000} (false) -> 20 @ 5
{0, 0, 1000, 10, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000} (false) -> 10 @ 3
{0, 0, 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000} (false) -> 0 @ 1
{0, 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000} (false) -> 0 @ 0
{1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000}


Associative (value-accessed)

Whereas sequences operate on positions (first, last, top, at a specific index), associative containers operate using the values of the elements.

Bag

Overview

Operations

Interface

interface Bag<E> {
	void add(T value)
	void remove(T value)
	bool contains(T value)
	boolean isEmpty
}

Motivation and Examples

Demo Code and Output

No such container in Java, so I'm showing you the demo:
import java.util.*;

public class BagDemo {
	public static void main(String [] args) {
		System.out.println("========== Bag ==========");
		Bag<Integer> bag = new Bag<>();

		System.out.println(bag + " (" + bag.isEmpty() + ")");
		System.out.println();

		System.out.println("Adding");
		System.out.println("------");
		for (int i = 0; i < 10; i++) {
			System.out.print((i * 10) + " -> "); 
			bag.add(i * 10);
			System.out.println(bag + " (" + bag.isEmpty() + ")");
		}

		System.out.println("Adding again");
		System.out.println("------------");
		for (int i = 0; i < 10; i++) {
			System.out.print((i * 10) + " -> "); 
			bag.add(i * 10);
			System.out.println(bag + " (" + bag.isEmpty() + ")");
		}

		System.out.println();
		System.out.println("Search by value");
		System.out.println("---------------");

		for (int i = 0; i < 20; i++) 
			System.out.println((i * 10) + ": " + bag.contains(i*10));

		System.out.println();
		System.out.println("Removing");
		System.out.println("--------");

		Random random = new Random(12345);
		while (!bag.isEmpty()) {
			int value = random.nextInt(10)*10;
			if (bag.contains(value)) {
				System.out.print(bag + " (" + bag.isEmpty() + ")");
				bag.remove(value);
				System.out.println(" -> " + value);
			}
		}
		System.out.println(bag + " (" + bag.isEmpty() + ")");
	}
}
Notes
What the demo is doing:

========== Bag ==========
{} (true)

Adding
------
0 -> {0} (false)
10 -> {0, 10} (false)
20 -> {0, 10, 20} (false)
30 -> {0, 10, 20, 30} (false)
40 -> {0, 10, 20, 30, 40} (false)
50 -> {0, 10, 20, 30, 40, 50} (false)
60 -> {0, 10, 20, 30, 40, 50, 60} (false)
70 -> {0, 10, 20, 30, 40, 50, 60, 70} (false)
80 -> {0, 10, 20, 30, 40, 50, 60, 70, 80} (false)
90 -> {0, 10, 20, 30, 40, 50, 60, 70, 80, 90} (false)
Adding again
------------
0 -> {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 0} (false)
10 -> {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 0, 10} (false)
20 -> {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 0, 10, 20} (false)
30 -> {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 0, 10, 20, 30} (false)
40 -> {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 0, 10, 20, 30, 40} (false)
50 -> {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 0, 10, 20, 30, 40, 50} (false)
60 -> {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 0, 10, 20, 30, 40, 50, 60} (false)
70 -> {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 0, 10, 20, 30, 40, 50, 60, 70} (false)
80 -> {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 0, 10, 20, 30, 40, 50, 60, 70, 80} (false)
90 -> {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90} (false)

Search by value
---------------
0: true
10: true
20: true
30: true
40: true
50: true
60: true
70: true
80: true
90: true
100: false
110: false
120: false
130: false
140: false
150: false
160: false
170: false
180: false
190: false

Removing
--------
{0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90} (false) -> 10
{0, 20, 30, 40, 50, 60, 70, 80, 90, 0, 20, 30, 40, 50, 60, 70, 80, 90} (false) -> 0
{20, 30, 40, 50, 60, 70, 80, 90, 20, 30, 40, 50, 60, 70, 80, 90} (false) -> 80
{20, 30, 40, 50, 60, 70, 90, 20, 30, 40, 50, 60, 70, 90} (false) -> 50
{20, 30, 40, 60, 70, 90, 20, 30, 40, 60, 70, 90} (false) -> 40
{20, 30, 60, 70, 90, 20, 30, 60, 70, 90} (false) -> 20
{30, 60, 70, 90, 30, 60, 70, 90} (false) -> 90
{30, 60, 70, 30, 60, 70} (false) -> 70
{30, 60, 30, 60} (false) -> 60
{30, 30} (false) -> 30
{} (true)


Set

Operations

Interface

interface Set<E> {
	void add(T value)
	void remove(T value)
	boolean isEmpty()
}

Motivation and Examples

Demo Code and Output

Notes
What the demo is doing:

========== Set ==========
{} (true)

Adding
------
0 -> {0} (false)
10 -> {0, 10} (false)
20 -> {0, 10, 20} (false)
30 -> {0, 10, 20, 30} (false)
40 -> {0, 10, 20, 30, 40} (false)
50 -> {0, 10, 20, 30, 40, 50} (false)
60 -> {0, 10, 20, 30, 40, 50, 60} (false)
70 -> {0, 10, 20, 30, 40, 50, 60, 70} (false)
80 -> {0, 10, 20, 30, 40, 50, 60, 70, 80} (false)
90 -> {0, 10, 20, 30, 40, 50, 60, 70, 80, 90} (false)
Adding again
------------
0 -> {0, 10, 20, 30, 40, 50, 60, 70, 80, 90} (false)
10 -> {0, 10, 20, 30, 40, 50, 60, 70, 80, 90} (false)
20 -> {0, 10, 20, 30, 40, 50, 60, 70, 80, 90} (false)
30 -> {0, 10, 20, 30, 40, 50, 60, 70, 80, 90} (false)
40 -> {0, 10, 20, 30, 40, 50, 60, 70, 80, 90} (false)
50 -> {0, 10, 20, 30, 40, 50, 60, 70, 80, 90} (false)
60 -> {0, 10, 20, 30, 40, 50, 60, 70, 80, 90} (false)
70 -> {0, 10, 20, 30, 40, 50, 60, 70, 80, 90} (false)
80 -> {0, 10, 20, 30, 40, 50, 60, 70, 80, 90} (false)
90 -> {0, 10, 20, 30, 40, 50, 60, 70, 80, 90} (false)

Membership testing
------------------
0: true
10: true
20: true
30: true
40: true
50: true
60: true
70: true
80: true
90: true
100: false
110: false
120: false
130: false
140: false
150: false
160: false
170: false
180: false
190: false

Removing
--------
{0, 10, 20, 30, 40, 50, 60, 70, 80, 90} (false) -> 10
{0, 20, 30, 40, 50, 60, 70, 80, 90} (false) -> 0
{20, 30, 40, 50, 60, 70, 80, 90} (false) -> 80
{20, 30, 40, 50, 60, 70, 90} (false) -> 50
{20, 30, 40, 60, 70, 90} (false) -> 40
{20, 30, 60, 70, 90} (false) -> 20
{30, 60, 70, 90} (false) -> 90
{30, 60, 70} (false) -> 70
{30, 60} (false) -> 60
{30} (false) -> 30
{} (true)


Map (Dictionary)

Different than the others; functionality is not quite that of the containers we've been looking at; rather a map is more of a lookup facility

Overview

Operations

interface Map<K, V> {
	void put(K key, V value)
	V get(K key)
	void remove(K key)
	bool contains(K key)
	boolean isEmpty()
}

Motivation and Examples

Demo Code and Output

Notes
What the demo is doing:

========== Map ==========
Putting
(10: 100) -> {(10, 100)}
(11: 121) -> {(10, 100), (11, 121)}
(12: 144) -> {(10, 100), (11, 121), (12, 144)}
(13: 169) -> {(10, 100), (11, 121), (12, 144), (13, 169)}
(14: 196) -> {(10, 100), (11, 121), (12, 144), (13, 169), (14, 196)}
(15: 225) -> {(10, 100), (11, 121), (12, 144), (13, 169), (14, 196), (15, 225)}
(16: 256) -> {(10, 100), (11, 121), (12, 144), (13, 169), (14, 196), (15, 225), (16, 256)}
(17: 289) -> {(10, 100), (11, 121), (12, 144), (13, 169), (14, 196), (15, 225), (16, 256), (17, 289)}
(18: 324) -> {(10, 100), (11, 121), (12, 144), (13, 169), (14, 196), (15, 225), (16, 256), (17, 289), (18, 324)}
(19: 361) -> {(10, 100), (11, 121), (12, 144), (13, 169), (14, 196), (15, 225), (16, 256), (17, 289), (18, 324), (19, 361)}

Getting

5 is not in the map
6 is not in the map
7 is not in the map
8 is not in the map
9 is not in the map
10 maps to 100 in the map
11 maps to 121 in the map
12 maps to 144 in the map
13 maps to 169 in the map
14 maps to 196 in the map
15 maps to 225 in the map
16 maps to 256 in the map
17 maps to 289 in the map
18 maps to 324 in the map
19 maps to 361 in the map
20 is not in the map
21 is not in the map
22 is not in the map
23 is not in the map
24 is not in the map

Removing
10 -> {(11, 121), (12, 144), (13, 169), (14, 196), (15, 225), (16, 256), (17, 289), (18, 324), (19, 361)}
11 -> {(12, 144), (13, 169), (14, 196), (15, 225), (16, 256), (17, 289), (18, 324), (19, 361)}
12 -> {(13, 169), (14, 196), (15, 225), (16, 256), (17, 289), (18, 324), (19, 361)}
13 -> {(14, 196), (15, 225), (16, 256), (17, 289), (18, 324), (19, 361)}
14 -> {(15, 225), (16, 256), (17, 289), (18, 324), (19, 361)}
15 -> {(16, 256), (17, 289), (18, 324), (19, 361)}
16 -> {(17, 289), (18, 324), (19, 361)}
17 -> {(18, 324), (19, 361)}
18 -> {(19, 361)}
19 -> {}

Sorted Associatives

Summary of Containers and Their Operations

Code Used in This Lecture