the container store

78
© 2001 by Ashby M. Woolf Revision 3 The Container Store A Place for Everything and Everything in its Place

Upload: kachina-baker

Post on 30-Dec-2015

22 views

Category:

Documents


0 download

DESCRIPTION

The Container Store. A Place for Everything and Everything in its Place. Containers. Arrays, Collections and Maps. Arrays of Primitives. public class ArraysOfPrimitives { public static void main(String[] args) { int[] a; int[] b = new int[] { 1, 2, 3 }; - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: The Container Store

© 2001 by Ashby M. Woolf Revision 3

The Container Store

A Place for Everything and Everything in its Place

Page 2: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Containers

Arrays,

Collections and

Maps

Page 3: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Arrays of Primitivespublic class ArraysOfPrimitives { public static void main(String[] args) { int[] a; int[] b = new int[] { 1, 2, 3 }; int[] c = new int[5]; for(int i = 0; i < 5; i++) { c[i] = i * 10; }//! System.out.println("a.length = " + a.length);//! for(int i = 0; i < a.length; i++) System.out.println(a[i]); System.out.println("b.length = " + b.length); for(int i = 0; i < b.length; i++) System.out.println(b[i]); System.out.println("c.length = " + c.length); for(int i = 0; i < c.length; i++) System.out.println(c[i]);//! c.length = 3; }}

Primitive Arrays initialized to zero.

Page 4: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Arrays of Primitivespublic class ArraysOfPrimitives { public static void main(String[] args) { int[] a; int[] b = new int[] { 1, 2, 3 }; int[] c = new int[5]; for(int i = 0; i < 5; i++) { c[i] = i * 10; }//! System.out.println("a.length = " + a.length);//! for(int i = 0; i < a.length; i++) System.out.println(a[i]); System.out.println("b.length = " + b.length); for(int i = 0; i < b.length; i++) System.out.println(b[i]); System.out.println("c.length = " + c.length); for(int i = 0; i < c.length; i++) System.out.println(c[i]);//! c.length = 3; }}

variable a might not have been initialized System.out.println("a.length = " + a.length); ^

Page 5: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Arrays of Primitivespublic class ArraysOfPrimitives { public static void main(String[] args) { int[] a; int[] b = new int[] { 1, 2, 3 }; int[] c = new int[5]; for(int i = 0; i < 5; i++) { c[i] = i * 10; }//! System.out.println("a.length = " + a.length);//! for(int i = 0; i < a.length; i++) System.out.println(a[i]); System.out.println("b.length = " + b.length); for(int i = 0; i < b.length; i++) System.out.println(b[i]); System.out.println("c.length = " + c.length); for(int i = 0; i < c.length; i++) System.out.println(c[i]);//! c.length = 3; }}

cannot assign a value to final variable length c.length = 3; ^

Page 6: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Arrays of Primitivespublic class ArraysOfPrimitives { public static void main(String[] args) { int[] a; int[] b = new int[] { 1, 2, 3 }; int[] c = new int[5]; for(int i = 0; i < 5; i++) { c[i] = i * 10; }//! System.out.println("a.length = " + a.length);//! for(int i = 0; i < a.length; i++) System.out.println(a[i]); System.out.println("b.length = " + b.length); for(int i = 0; i < b.length; i++) System.out.println(b[i]); System.out.println("c.length = " + c.length); for(int i = 0; i < c.length; i++) System.out.println(c[i]);//! c.length = 3; }}

b.length = 3123c.length = 5010203040

Page 7: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Arrays Verses the other Containers

• Most Efficient

• Type Specific

• Size Fixed at Creation

• Bounds Checking

• Other Containers Don't Hold Primitives Directly

Page 8: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Arrays are Objects

• An Array is an Object • An Array Identifier is a Reference to an Object

• An Array is an Object• An Array Identifier is a Reference to an Object

• An Array is an Object• An Array Identifier is a Reference to an Object

• An Array is an Object• An Array Identifier is a Reference to an Object

Page 9: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Arrays of Objectspublic class ArraysOfObjects { public static void main(String[] args) { String[] a; String[] b = new String[] { "one", "two", "three" }; String[] c = new String[5]; for(int i = 0; i < 5; i++) { c[i] = "Value = " + (i * 10); }//! System.out.println("a.length = " + a.length);//! for(int i = 0; i < a.length; i++) System.out.println(a[i]); System.out.println("b.length = " + b.length); for(int i = 0; i < b.length; i++) System.out.println(b[i]); System.out.println("c.length = " + c.length); for(int i = 0; i < c.length; i++) System.out.println(c[i]);//! c.length = 3; }}

Arrays of Objects initialized to null.

Page 10: The Container Store

© 2001 by Ashby M. Woolf Revision 3

public class ArraysOfObjects { public static void main(String[] args) { String[] a; String[] b = new String[] { "one", "two", "three" }; String[] c = new String[5]; for(int i = 0; i < 5; i++) { c[i] = "Value = " + (i * 10); }//! System.out.println("a.length = " + a.length);//! for(int i = 0; i < a.length; i++) System.out.println(a[i]); System.out.println("b.length = " + b.length); for(int i = 0; i < b.length; i++) System.out.println(b[i]); System.out.println("c.length = " + c.length); for(int i = 0; i < c.length; i++) System.out.println(c[i]);//! c.length = 3; }}

Arrays of Objects

variable a might not have been initialized System.out.println("a.length = " + a.length); ^

Page 11: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Arrays of Objectspublic class ArraysOfObjects { public static void main(String[] args) { String[] a; String[] b = new String[] { "one", "two", "three" }; String[] c = new String[5]; for(int i = 0; i < 5; i++) { c[i] = "Value = " + (i * 10); }//! System.out.println("a.length = " + a.length);//! for(int i = 0; i < a.length; i++) System.out.println(a[i]); System.out.println("b.length = " + b.length); for(int i = 0; i < b.length; i++) System.out.println(b[i]); System.out.println("c.length = " + c.length); for(int i = 0; i < c.length; i++) System.out.println(c[i]);//! c.length = 3; }}

cannot assign a value to final variable length c.length = 3; ^

Page 12: The Container Store

© 2001 by Ashby M. Woolf Revision 3

public class ArraysOfObjects { public static void main(String[] args) { String[] a; String[] b = new String[] { "one", "two", "three" }; String[] c = new String[5]; for(int i = 0; i < 5; i++) { c[i] = "Value = " + (i * 10); }//! System.out.println("a.length = " + a.length);//! for(int i = 0; i < a.length; i++) System.out.println(a[i]); System.out.println("b.length = " + b.length); for(int i = 0; i < b.length; i++) System.out.println(b[i]); System.out.println("c.length = " + c.length); for(int i = 0; i < c.length; i++) System.out.println(c[i]);//! c.length = 3; }}

Arrays of Objects

b.length = 3onetwothreec.length = 5Value = 0Value = 10Value = 20Value = 30Value = 40

Page 13: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Returning an ArrayAn Array is an Object

class A { public String[] meth() { String[] s = new String[] {"one", "two", "three"}; return s; } • • •}

class B { • • • String[] str; A a = new A() str = a.meth(); • • •}

Page 14: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Array Utilitiesjava.util.Arrays

Page 15: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Copying an Arrayjava.lang.System

public static void arraycopy(Object src,

int src_posit,

Object dst,

int dst_posit,

int length)

Parameters:

src - Source Array

src_posit - Source Array starting position

dst - Destination Array

dst_posit - Destination Array starting position

length - number of components copied.

Page 16: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Filling an Arrayjava.util.Arrays

public static void fill(long[] a,

int fromIndex,

int toIndex,

long val)

Parameters:a - the array to be filled.

fromIndex - index of first element (inclusive) to be filled

toIndex - index of last element (exclusive) to be filled

val - the value to be stored

Page 17: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Comparing Arraysjava.util.Arrays

public static boolean equals(int[] a1,

int[] a2) • • •public static boolean equals(Object[] a1,

Object[] a2)

Parameters:

a1 - one array to be tested for equality.

a2 - the other array to be tested for equality.

Returns:

true if the two arrays are equal. For Objects: (e1==null ? e2==null : e1.equals(e2))

Page 18: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Sorting Arraysjava.util.Arrays

public static void sort(int[] a)

public static void sort(int[] a, int from, int to)

• • •public static void sort(Object[] a)

public static void sort(Object[] a, int from, int to)

Parameters:

a - array to be sorted.

from - first element to be sorted (inclusive).

to - index of end of range to be sorted (exclusive).

Page 19: The Container Store

© 2001 by Ashby M. Woolf Revision 3

How do we compare Arrays of Objects

• Two ways to provide compare functionality

• Array Objects Implement Comparable– "The natural ordering of elements"

– public int compareTo(Object o)

– public static void sort(Object[] a)

• Provide a Class that Implements Comparator– public int compare(Object o1, Object o2)

– public static void sort(Object[] a, Comparator c)

Page 20: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Searching an Arrayjava.util.Arrays

int binarySearch(int[] a, int key) • • •int binarySearch(Object[] a, Object key)

int binarySearch(Object[] a, Object key,Comparator c)

Parameters:a - the array to be searched (must be sorted).

key - the value being searched for

c - the Comparator by which the array is sorted

Page 21: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Array Summary

• Size determined at creation

• Type specific

• Uses TypeName[ ] syntax

• Fast

Page 22: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Arrays

A Change of Topic

Introductionto

Containers

Page 23: The Container Store

© 2001 by Ashby M. Woolf Revision 3

A Review - Interfaces, Abstract Classes, and Classes

class Bird extends Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Tweet"); } }}

class Dog extends Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Woof"); } }}

interface Pet { String name; void speak(int i); void setName(String s) { name = s; } String getName() { return name; }}

Page 24: The Container Store

© 2001 by Ashby M. Woolf Revision 3

A Review - Interfaces, Abstract Classes, and Classes

class Bird extends Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Tweet"); } }}

class Dog extends Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Woof"); } }}

interface Pet { String name; void speak(int i); void setName(String s) { name = s; } String getName() { return name; }}

abstract class APet implements Pet{

}

abstract class APet implements Pet{ String name;

}

interface Pet { void speak(int i); void setName(String s) { name = s; } String getName() { return name; }}

abstract class APet implements Pet{ String name; public void setName(String s) { name = s; }

}

interface Pet { void speak(int i); void setName(String s); String getName() { return name; }}

abstract class APet implements Pet{ String name; public void setName(String s) { name = s; } public String getName() { return name; }}

interface Pet { void speak(int i); void setName(String s); String getName();}

abstract class APet implements Pet{ String name; public void setName(String s) { name = s; } public String getName() { return name; }}

class Bird extends APet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Tweet"); } }}

class Dog extends APet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Woof"); } }}

class Bird extends APet { public void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Tweet"); } }}

class Dog extends APet { public void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Woof"); } }}

class Bird extends APet { public void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Tweet"); } }}

class Dog extends APet { public void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Woof"); } }}

Page 25: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Collection

Set

List

AbstractCollection

AbstractListAbstractSet

SortedSet

AbstractSequentialList

HashMapTreeSetHashSetLinkedListArrayList TreeMapWeakHashMap

SortedMap

Map

AbstractMap

Collection

Set

ListSortedSet SortedMap

Map

AbstractCollection

AbstractListAbstractSet

AbstractSequentialList

AbstractMap

HashMapTreeSetHashSetLinkedListArrayList TreeMapWeakHashMap

The Containers

Page 26: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Collection

Set

List

AbstractCollection

AbstractListAbstractSet

SortedSet

AbstractSequentialList

HashMapTreeSetHashSetLinkedListArrayList TreeMapWeakHashMap

SortedMap

Map

AbstractMap

The Containers

TreeSetHashSetLinkedListArrayList

Page 27: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Introductionto

Containers

A Change of Topic

Collections

Page 28: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Object Object Object Object Object Object • • •

Object F B

Object F BObject nu

ll

B

Object F null

Object L R

Object L RObject L R

Object

Object

ObjectObjectObject

Object

0123456•••n

ArrayList

TreeSet

LinkedList

HashSet

The Collections

1

0

1

0

0

1

1

1

0

1

0

Object null

null Object null

null

Object null

null

Object null

null

Page 29: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Data Structure Reference

http://swww.ee.uwa.edu.au/~plsd210/ds/

Page 30: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Collection

Set

List

AbstractCollection

AbstractListAbstractSet

SortedSet

AbstractSequentialList

HashMapTreeSetHashSetLinkedListArrayList TreeMapWeakHashMap

SortedMap

Map

AbstractMap

The Containers

boolean add(Object o);boolean remove(Object o);void clear();boolean isEmpty();boolean contains(Object o);boolean containsAll(Collection c);boolean addAll(Collection c);boolean removeAll(Collection c);boolean retainAll(Collection c);Iterator iterator();

Page 31: The Container Store

© 2001 by Ashby M. Woolf Revision 3

A Utility for Filling Collectionsclass Builder {

static void stuffItA(Collection c) {c.add("alpha");c.add("gamma");c.add("delta");c.add("beta");return;

}

static void stuffItB(Collection c) {c.add("beta");c.add("four");c.add("five");c.add("delta");return;

}}

Page 32: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Collection

Set

List

AbstractCollection

AbstractListAbstractSet

SortedSet

AbstractSequentialList

HashMapTreeSetHashSetLinkedListArrayList TreeMapWeakHashMap

SortedMap

Map

AbstractMap

The ContainersCollection

TreeSetHashSetLinkedListArrayList

Page 33: The Container Store

© 2001 by Ashby M. Woolf Revision 3

A Utility for Filling Collectionsclass Builder {

static void stuffItA(Collection c) {c.add("alpha");c.add("gamma");c.add("delta");c.add("beta");return;

}

static void stuffItB(Collection c) {c.add("beta");c.add("four");c.add("five");c.add("delta");return;

}}

Page 34: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Demonstrating the Collectionspublic class DoArrayList { public static void main(String[] args) { Collection a; Collection b; boolean bool; Iterator it; ListIterator lit; Object obj = new String("delta");

System.out.println("\nContainer a is an ArrayList"); a = new ArrayList(); Builder.stuffItA(a); System.out.println("Container b is a ArrayList\n"); b = new ArrayList(); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b);// Demo Operations Here * * * * * }}

Container a is an ArrayListContainer b is a ArrayList

a = [alpha, gamma, delta, beta]b = [beta, four, five, delta]

Hiding the declarationsto make space.

Page 35: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Demonstrating the Collectionspublic class DoArrayList { public static void main(String[] args) { Collection a; Collection b; boolean bool; Iterator it; ListIterator lit;

System.out.println("\nContainer a is an ArrayList"); a = new ArrayList(); Builder.stuffItA(a); System.out.println("Container b is a ArrayList\n"); b = new ArrayList(); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b);// Demo Operations Here * * * * * }}

public class DoArrayList { public static void main(String[] args) { Collection a; Collection b; boolean bool; Iterator it;

System.out.println("\nContainer a is an ArrayList"); a = new ArrayList(); Builder.stuffItA(a); System.out.println("Container b is a ArrayList\n"); b = new ArrayList(); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b);// Demo Operations Here * * * * * }}

public class DoArrayList { public static void main(String[] args) { Collection a; Collection b; boolean bool;

System.out.println("\nContainer a is an ArrayList"); a = new ArrayList(); Builder.stuffItA(a); System.out.println("Container b is a ArrayList\n"); b = new ArrayList(); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b);// Demo Operations Here * * * * * }}

public class DoArrayList { public static void main(String[] args) { Collection a; Collection b;

System.out.println("\nContainer a is an ArrayList"); a = new ArrayList(); Builder.stuffItA(a); System.out.println("Container b is a ArrayList\n"); b = new ArrayList(); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b);// Demo Operations Here * * * * * }}

public class DoArrayList { public static void main(String[] args) { Collection a;

System.out.println("\nContainer a is an ArrayList"); a = new ArrayList(); Builder.stuffItA(a); System.out.println("Container b is a ArrayList\n"); b = new ArrayList(); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b);// Demo Operations Here * * * * * }}

public class DoArrayList { public static void main(String[] args) { // Hidden Declarations

System.out.println("\nContainer a is an ArrayList"); a = new ArrayList(); Builder.stuffItA(a); System.out.println("Container b is a ArrayList\n"); b = new ArrayList(); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b);// Demo Operations Here * * * * * }}

Container a is an ArrayListContainer b is a ArrayList

a = [alpha, gamma, delta, beta]b = [beta, four, five, delta]

Page 36: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Collection

Set

List

AbstractCollection

AbstractListAbstractSet

SortedSet

AbstractSequentialList

HashMapTreeSetHashSetLinkedListArrayList TreeMapWeakHashMap

SortedMap

Map

AbstractMap

The ContainersCollection

addAll()defined in theCollections interface.

TreeSetHashSetLinkedListArrayList

Page 37: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Demonstrating addAll() with ArrayListpublic class DoArrayList { public static void main(String[] args) { // Hidden Declarations

System.out.println("\nContainer a is an ArrayList"); a = new ArrayList(); Builder.stuffItA(a); System.out.println("Container b is a ArrayList\n"); b = new ArrayList(); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b);

}}

public class DoArrayList { public static void main(String[] args) { // Hidden Declarations

System.out.println("\nContainer a is an ArrayList"); a = new ArrayList(); Builder.stuffItA(a); System.out.println("Container b is a ArrayList\n"); b = new ArrayList(); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b);

a.addAll( b ); System.out.println("a = " + a); }} Container a is an ArrayList

Container b is a ArrayList

a = [alpha, gamma, delta, beta]b = [beta, four, five, delta]a = [alpha, gamma, delta, beta, beta, four, five, delta]

Object Object Object • • •

ArrayList

Page 38: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Demonstrating addAll() with LinkedListpublic class DoArrayList { public static void main(String[] args) { // Hidden Declarations

System.out.println("\nContainer a is an ArrayList"); a = new ArrayList(); Builder.stuffItA(a); System.out.println("Container b is a ArrayList\n"); b = new ArrayList(); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b);

a.addAll( b ); System.out.println("a = " + a); }}

public class DoLinkedList { public static void main(String[] args) { // Hidden Declarations

System.out.println("\nContainer a is an LinkedList"); a = new LinkedList(); Builder.stuffItA(a); System.out.println("Container b is a LinkedList \n"); b = new LinkedList(); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b);

a.addAll( b ); System.out.println("a = " + a); }}

Container a is an LinkedListContainer b is a LinkedList

a = [alpha, gamma, delta, beta]b = [beta, four, five, delta]a = [alpha, gamma, delta, beta, beta, four, five, delta]

Object F B

Object nu

ll

B

Object F nu

ll

LinkedList

Page 39: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Demonstrating addAll() with TreeSetpublic class DoLinkedList { public static void main(String[] args) { // Hidden Declarations

System.out.println("\nContainer a is an LinkedList"); a = new LinkedList(); Builder.stuffItA(a); System.out.println("Container b is a LinkedList \n"); b = new LinkedList(); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b);

a.addAll( b ); System.out.println("a = " + a); }}

public class DoTreeSet { public static void main(String[] args) { // Hidden Declarations

System.out.println("\nContainer a is an TreeSet"); a = new TreeSet (); Builder.stuffItA(a); System.out.println("Container b is a TreeSet \n"); b = new TreeSet (); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b);

a.addAll( b ); System.out.println("a = " + a); }}

Container a is an TreeSetContainer b is a TreeSet

a = [alpha, beta, delta, gamma]b = [beta, delta, five, four]a = [alpha, beta, delta, five, four, gamma]

Sorted and No Duplicates.

Object L RTreeSet

Object nu

lln

ull

Object nu

lln

ull

Page 40: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Demonstrating addAll() with HashSetpublic class DoTreeSet { public static void main(String[] args) { // Hidden Declarations

System.out.println("\nContainer a is an TreeSet"); a = new TreeSet (); Builder.stuffItA(a); System.out.println("Container b is a TreeSet \n"); b = new TreeSet (); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b);

a.addAll( b ); System.out.println("a = " + a); }}

public class DoHashSet { public static void main(String[] args) { // Hidden Declarations

System.out.println("\nContainer a is an HashSet"); a = new HashSet (); Builder.stuffItA(a); System.out.println("Container b is a HashSet \n"); b = new HashSet (); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b);

a.addAll( b ); System.out.println("a = " + a); }}

Container a is an HashSetContainer b is a HashSet

a = [gamma, beta, alpha, delta]b = [five, four, beta, delta]a = [gamma, five, four, beta, alpha, delta]

No Duplicates.

HashSet0

1

2

n

Object

Object

Object

Object

1

0

1

1

0

1

0

Page 41: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Collection

Set

List

AbstractCollection

AbstractListAbstractSet

SortedSet

AbstractSequentialList

HashMapTreeSetHashSetLinkedListArrayList TreeMapWeakHashMap

SortedMap

Map

AbstractMap

The ContainersCollection

removeAll()in theCollections interface.

TreeSetHashSetLinkedListArrayList

Page 42: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Demonstrating removeAll() with ArrayListpublic class DoArrayList { public static void main(String[] args) { // Hidden Declarations

System.out.println("\nContainer a is an ArrayList"); a = new ArrayList(); Builder.stuffItA(a); System.out.println("Container b is a ArrayList\n"); b = new ArrayList(); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b); a.addAll(b); System.out.println("a = " + a); }}

public class DoArrayList { public static void main(String[] args) { // Hidden Declarations

System.out.println("\nContainer a is an ArrayList"); a = new ArrayList(); Builder.stuffItA(a); System.out.println("Container b is a ArrayList\n"); b = new ArrayList(); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b);

a.removeAll( b ); System.out.println("a = " + a); }} Container a is an ArrayList

Container b is an ArrayList

a = [alpha, gamma, delta, beta]b = [beta, four, five, delta]a = [alpha, gamma]

Object Object Object • • •

ArrayList

Page 43: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Demonstrating removeAll() with LinkedListpublic class DoArrayList { public static void main(String[] args) { // Hidden Declarations

System.out.println("\nContainer a is an ArrayList"); a = new ArrayList(); Builder.stuffItA(a); System.out.println("Container b is a ArrayList\n"); b = new ArrayList(); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b);

a.removeAll( b ); System.out.println("a = " + a); }}

public class DoLinkedList { public static void main(String[] args) { // Hidden Declarations

System.out.println("\nContainer a is an LinkedList"); a = new LinkedList(); Builder.stuffItA(a); System.out.println("Container b is a LinkedList \n"); b = new LinkedList(); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b);

a.removeAll( b ); System.out.println("a = " + a); }}

Container a is an LinkedListContainer b is an LinkedList

a = [alpha, gamma, delta, beta]b = [beta, four, five, delta]a = [alpha, gamma]

Object F B

Object nu

ll

B

Object F nu

ll

LinkedList

Page 44: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Demonstrating removeAll() with TreeSetpublic class DoLinkedList { public static void main(String[] args) { // Hidden Declarations

System.out.println("\nContainer a is an LinkedList"); a = new LinkedList(); Builder.stuffItA(a); System.out.println("Container b is a LinkedList \n"); b = new LinkedList(); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b);

a.removeAll( b ); System.out.println("a = " + a); }}

public class DoTreeSet { public static void main(String[] args) { // Hidden Declarations

System.out.println("\nContainer a is an TreeSet"); a = new TreeSet (); Builder.stuffItA(a); System.out.println("Container b is a TreeSet \n"); b = new TreeSet (); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b);

a.removeAll( b ); System.out.println("a = " + a); }}

Container a is an TreeSetContainer b is an TreeSet

a = [alpha, beta, delta, gamma]b = [beta, delta, five, four]a = [alpha, gamma]

Sorted and No Duplicates.

Object L RTreeSet

Object nu

lln

ull

Object nu

lln

ull

Page 45: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Demonstrating removeAll() with HashSetpublic class DoTreeSet { public static void main(String[] args) { // Hidden Declarations

System.out.println("\nContainer a is an TreeSet"); a = new TreeSet (); Builder.stuffItA(a); System.out.println("Container b is a TreeSet \n"); b = new TreeSet (); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b);

a.removeAll( b ); System.out.println("a = " + a); }}

public class DoHashSet { public static void main(String[] args) { // Hidden Declarations

System.out.println("\nContainer a is an HashSet"); a = new HashSet (); Builder.stuffItA(a); System.out.println("Container b is a HashSet \n"); b = new HashSet (); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b);

a.removeAll( b ); System.out.println("a = " + a); }}

Container a is an HashSetContainer b is an HashSet

a = [gamma, beta, alpha, delta]b = [five, four, beta, delta]a = [gamma, alpha]

No Duplicates.

HashSet0

1

2

n

Object

Object

Object

Object

1

0

1

1

0

1

0

Page 46: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Demonstrating retainAll() with ArrayListpublic class DoArrayList { public static void main(String[] args) { // Hidden Declarations

System.out.println("\nContainer a is an ArrayList"); a = new ArrayList(); Builder.stuffItA(a); System.out.println("Container b is a ArrayList\n"); b = new ArrayList(); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b); a.removeAll(b); System.out.println("a = " + a); }}

public class DoArrayList { public static void main(String[] args) { // Hidden Declarations

System.out.println("\nContainer a is an ArrayList"); a = new ArrayList(); Builder.stuffItA(a); System.out.println("Container b is a ArrayList\n"); b = new ArrayList(); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b);

a.retainAll( b ); System.out.println("a = " + a); }} Container a is an ArrayList

Container b is an ArrayList

a = [alpha, gamma, delta, beta]b = [beta, four, five, delta]a = [delta, beta]

Object Object Object • • •

ArrayList

Page 47: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Demonstrating retainAll() with LinkedListpublic class DoArrayList { public static void main(String[] args) { // Hidden Declarations

System.out.println("\nContainer a is an ArrayList"); a = new ArrayList(); Builder.stuffItA(a); System.out.println("Container b is a ArrayList\n"); b = new ArrayList(); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b);

a.retainAll( b ); System.out.println("a = " + a); }}

public class DoLinkedList { public static void main(String[] args) { // Hidden Declarations

System.out.println("\nContainer a is an LinkedList"); a = new LinkedList(); Builder.stuffItA(a); System.out.println("Container b is a LinkedList \n"); b = new LinkedList(); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b);

a.retainAll( b ); System.out.println("a = " + a); }}

Container a is an LinkedListContainer b is an LinkedList

a = [alpha, gamma, delta, beta]b = [beta, four, five, delta]a = [delta, beta]

Object F B

Object nu

ll

B

Object F nu

ll

LinkedList

Page 48: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Demonstrating retainAll() with TreeSetpublic class DoLinkedList { public static void main(String[] args) { // Hidden Declarations

System.out.println("\nContainer a is an LinkedList"); a = new LinkedList(); Builder.stuffItA(a); System.out.println("Container b is a LinkedList \n"); b = new LinkedList(); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b);

a.retainAll( b ); System.out.println("a = " + a); }}

public class DoTreeSet { public static void main(String[] args) { // Hidden Declarations

System.out.println("\nContainer a is an TreeSet"); a = new TreeSet (); Builder.stuffItA(a); System.out.println("Container b is a TreeSet \n"); b = new TreeSet (); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b);

a.retainAll( b ); System.out.println("a = " + a); }}

Container a is an TreeSetContainer b is an TreeSet

a = [alpha, beta, delta, gamma]b = [beta, delta, five, four]a = [beta, delta]

Sorted and No Duplicates.

Object L RTreeSet

Object nu

lln

ull

Object nu

lln

ull

Page 49: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Demonstrating retainAll() with HashSetpublic class DoTreeSet { public static void main(String[] args) { // Hidden Declarations

System.out.println("\nContainer a is an TreeSet"); a = new TreeSet (); Builder.stuffItA(a); System.out.println("Container b is a TreeSet \n"); b = new TreeSet (); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b);

a.retainAll( b ); System.out.println("a = " + a); }}

public class DoHashSet { public static void main(String[] args) { // Hidden Declarations

System.out.println("\nContainer a is an HashSet"); a = new HashSet (); Builder.stuffItA(a); System.out.println("Container b is a HashSet \n"); b = new HashSet (); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b);

a.retainAll( b ); System.out.println("a = " + a); }}

Container a is an HashSetContainer b is an HashSet

a = [gamma, beta, alpha, delta]b = [five, four, beta, delta]a = [beta, delta]

No Duplicates.

HashSet0

1

2

n

Object

Object

Object

Object

1

0

1

1

0

1

0

Page 50: The Container Store

© 2001 by Ashby M. Woolf Revision 3

public class DoArrayList { public static void main(String[] args) { // Hidden Declarations

System.out.println("\nContainer a is an ArrayList"); a = new ArrayList(); Builder.stuffItA(a); System.out.println("Container b is a ArrayList\n"); b = new ArrayList(); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b);

a.retainAll( b ); System.out.println("a = " + a); }}

Working with a Single Containerpublic class DoArrayList { public static void main(String[] args) { // Hidden Declarations

System.out.println("\nContainer a is an ArrayList"); a = new ArrayList(); Builder.stuffItA(a); System.out.println("a = " + a); }}

Container a is an ArrayLista = [alpha, gamma, delta, beta]

Demo stuff goes here

Page 51: The Container Store

© 2001 by Ashby M. Woolf Revision 3

interface ListReader ( int read();}class List { private int[] list = new int[100]; private int addPoint = 0; public void add(int i) { list[addPoint++] = i; } public ListReader getReader() { return new Reader(); } private class Reader implements ListReader { private int readPoint = 0; public int read() { return list[readPoint++]; } }}public class DoList { public static void main(String[] args) { List l = new List(); ListReader r1 = l.getReader(); ListReader r2 = l.getReader(); int k; for( k = 0; k < 5; k++ ) { l.add( k * 10 ); } for( k = 0; k < 5; k++ ) { System.out.println(r1.read()); } for( k = 0; k < 5; k++ ) { System.out.println(r2.read()); } }}

010203040010203040

A Simple Containerfrom the Past

Page 52: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Your Classes

Library Classes

Using Class

• Make a Outer Class• Get an inner class and reference made for you• Apply the Inner class methods

The Closure

The Outer Classwith A Method

to return an object reference to an

inner class

An Interface

specifies the inner class method interface

"List"with A Method

to return an object "getReader()"

"DoList"

• Make a Outer Class• Get an inner class and reference made for you• Apply the Inner class methods

r

Inner Classprovides controlled

access to outerclass information

"Reader"provides controlled

access to outerclass information

"ListReader"

specifies the inner class method interface

Page 53: The Container Store

© 2001 by Ashby M. Woolf Revision 3

public class DoArrayList { public static void main(String[] args) { // Hidden Declarations

System.out.println("\nContainer a is an ArrayList"); a = new ArrayList(); Builder.stuffItA(a); System.out.println("Container b is a ArrayList\n"); b = new ArrayList(); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b);

a.retainAll( b ); System.out.println("a = " + a); }}

Working with a Single Containerpublic class DoArrayList { public static void main(String[] args) { // Hidden Declarations

System.out.println("\nContainer a is an ArrayList"); a = new ArrayList(); Builder.stuffItA(a); System.out.println("a = " + a); }}

Container a is an ArrayLista = [alpha, gamma, delta, beta]

Page 54: The Container Store

© 2001 by Ashby M. Woolf Revision 3

iterator(), hasNext(), next() with ArrayListpublic class DoArrayList { public static void main(String[] args) { // Hidden Declarations

System.out.println("\nContainer a is an ArrayList"); a = new ArrayList(); Builder.stuffItA(a); System.out.println("a = " + a); System.out.println("Iterate through an Iterator"); it = a.iterator(); while(it.hasNext()) { System.out.println(" " + it.next()); } }}

Container a is an ArrayLista = [alpha, gamma, delta, beta]Iterate through an Iterator alpha gamma delta beta

Page 55: The Container Store

© 2001 by Ashby M. Woolf Revision 3

The Iterator Interface

• boolean hasNext() – Returns true if the iteration has more elements.

• Object next() – Returns the next element in the interation.

• void remove() – Removes from the underlying collection the last element

returned by the iterator (optional operation).

Page 56: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Collection

Set

List

AbstractCollection

AbstractListAbstractSet

SortedSet

AbstractSequentialList

HashMapTreeSetHashSetLinkedListArrayList TreeMapWeakHashMap

SortedMap

Map

AbstractMap

The Containers

Iterator iterator();

Collection

TreeSetHashSetLinkedListArrayList

Page 57: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Collection

Set

List

AbstractCollection

AbstractListAbstractSet

SortedSet

AbstractSequentialList

HashMapTreeSetHashSetLinkedListArrayList TreeMapWeakHashMap

SortedMap

Map

AbstractMap

The Containers

ListIterator listIterator();

List

LinkedListArrayList

Page 58: The Container Store

© 2001 by Ashby M. Woolf Revision 3

ListIterators• void add(Object o)

– Inserts the specified element into the list (optional operation).

• boolean hasNext() – Returns true if this list iterator has more elements when traversing

the list in the forward direction.

• Object next() – Returns the next element in the list.

• int nextIndex() – Returns the index of the element that would be returned by a

subsequent call to next.

• void remove() – Removes from the list the last element that was returned by next

or previous (optional operation).

• void set(Object o) – Replaces the last element returned by next or previous with the

specified element (optional operation).

Page 59: The Container Store

© 2001 by Ashby M. Woolf Revision 3

ListIterators (Cont.)

• boolean hasPrevious() – Returns true if this list iterator has more elements when

traversing the list in the reverse direction.

• Object previous() – Returns the previous element in the list.

• int previousIndex() – Returns the index of the element that would be returned by a

subsequent call to previous.

Page 60: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Collection

Set

List

AbstractCollection

AbstractListAbstractSet

SortedSet

AbstractSequentialList

HashMapTreeSetHashSetLinkedListArrayList TreeMapWeakHashMap

SortedMap

Map

AbstractMap

The Containers

HashMap TreeMap

Page 61: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Collections

A Change of Topic

Maps

Page 62: The Container Store

© 2001 by Ashby M. Woolf Revision 3

TreeMap

HashMapKey

Key

KeyKeyKey

Key

0123456•••n

Data

Data

DataDataData

Data

DataL RKey

DataL RKeyDataL RKey

The Maps

DataKey null

null

DataKey null

null

DataKey null

null

DataKey null

null

Page 63: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Collection

Set

List

AbstractCollection

AbstractListAbstractSet

SortedSet

AbstractSequentialList

HashMapTreeSetHashSetLinkedListArrayList TreeMapWeakHashMap

SortedMap

Map

AbstractMap

The Containers

void clear() boolean containsKey(Object key) boolean containsValue(Object value) Set entrySet() boolean equals(Object o) Object get(Object key) int hashCode() boolean isEmpty() Set keySet() Object put(Object key, Object value) void putAll(Map t) Object remove(Object key) int size() Collection values()

Map

HashMap TreeMapWeakHashMap

void clear() boolean containsKey(Object key) boolean containsValue(Object value) Set entrySet() boolean equals(Object o) Object get(Object key) int hashCode() boolean isEmpty() Set keySet() Object put(Object key, Object value) void putAll(Map t) Object remove(Object key) int size() Collection values()

Page 64: The Container Store

© 2001 by Ashby M. Woolf Revision 3

A Utility for Filling Mapsclass Builder {

static void stuffItA(Collection c) {c.add("alpha");c.add("gamma");c.add("delta");c.add("beta");return;

}

static void stuffItB(Collection c) {c.add("beta");c.add("four");c.add("five");c.add("delta");return;

}}

class Builder {static void stuffItA(Collection c) {

c.put("a", "alpha");c.put("g", "gamma");c.put("d", "delta");c.put("b", "beta");return;

}

static void stuffItB(Collection c) {c.put("b", "beta");c.put("4", "four");c.put("5", "five");c.put("d", "delta");return;

}}

class Builder {static void stuffItA(Map c) {

c.put("a", "alpha");c.put("g", "gamma");c.put("d", "delta");c.put("b", "beta");return;

}

static void stuffItB(Map c) {c.put("b", "beta");c.put("4", "four");c.put("5", "five");c.put("d", "delta");return;

}}

Page 65: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Collection

Set

List

AbstractCollection

AbstractListAbstractSet

SortedSet

AbstractSequentialList

HashMapTreeSetHashSetLinkedListArrayList TreeMapWeakHashMap

SortedMap

Map

AbstractMap

The ContainersMap

HashMap TreeMapWeakHashMap

Page 66: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Collection

Set

List

AbstractCollection

AbstractListAbstractSet

SortedSet

AbstractSequentialList

HashMapTreeSetHashSetLinkedListArrayList TreeMapWeakHashMap

SortedMap

Map

AbstractMap

The Containers

void clear() boolean containsKey(Object key) boolean containsValue(Object value) Set entrySet() boolean equals(Object o) Object get(Object key) int hashCode() boolean isEmpty() Set keySet() Object put(Object key, Object value) void putAll(Map t) Object remove(Object key) int size() Collection values()

Map

HashMap TreeMapWeakHashMap

void clear() boolean containsKey(Object key) boolean containsValue(Object value) Set entrySet() boolean equals(Object o) Object get(Object key) int hashCode() boolean isEmpty() Set keySet() Object put(Object key, Object value) void putAll(Map t) Object remove(Object key) int size() Collection values()

Page 67: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Demonstrating putAll() with TreeMappublic class DoTreeMap { public static void main(String[] args) { // Hidden Declarations

System.out.println("\nContainer a is an TreeMap"); a = new TreeMap(); Builder.stuffItA(a); System.out.println("Container b is a TreeMap\n"); b = new TreeMap(); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b);

}}

public class DoTreeMap { public static void main(String[] args) { // Hidden Declarations

System.out.println("\nContainer a is an TreeMap"); a = new TreeMap(); Builder.stuffItA(a); System.out.println("Container b is a TreeMap\n"); b = new TreeMap(); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b);

a.putAll( b ); System.out.println("a = " + a); }}

Container a is an TreeMapContainer b is a TreeMap

a = {a=alpha, b=beta, d=delta, g=gamma}b = {4=four, 5=five, b=beta, d=delta}a = {4=four, 5=five, a=alpha, b=beta, d=delta, g=gamma}

TreeMap

DataL RKey

DataKey nu

lln

ull

DataKey nu

lln

ull

Page 68: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Demonstrating putAll() with HashMappublic class DoHashMap { public static void main(String[] args) { // Hidden Declarations

System.out.println("\nContainer a is an HashMap"); a = new HashMap(); Builder.stuffItA(a); System.out.println("Container b is a HashMap\n"); b = new HashMap(); Builder.stuffItB(b); System.out.println("a = " + a); System.out.println("b = " + b);

a.putAll( b ); System.out.println("a = " + a); }}

Container a is an HashMapContainer b is a HashMap

a = {b=beta, a=alpha, g=gamma, d=delta}b = {b=beta, 5=five, 4=four, d=delta}a = {b=beta, 5=five, a=alpha, 4=four, g=gamma, d=delta}

0

1

2

n

HashMapKey

Key

Key

Key

Data

Data

Data

Data

Page 69: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Collection

Set

List

AbstractCollection

AbstractListAbstractSet

SortedSet

AbstractSequentialList

HashMapTreeSetHashSetLinkedListArrayList TreeMapWeakHashMap

SortedMap

Map

AbstractMap

The Containers

void clear() boolean containsKey(Object key) boolean containsValue(Object value) Set entrySet() boolean equals(Object o) Object get(Object key) int hashCode() boolean isEmpty() Set keySet() Object put(Object key, Object value) void putAll(Map t) Object remove(Object key) int size() Collection values()

Map

HashMap TreeMapWeakHashMap

void clear() boolean containsKey(Object key) boolean containsValue(Object value) Set entrySet() boolean equals(Object o) Object get(Object key) int hashCode() boolean isEmpty() Set keySet() Object put(Object key, Object value) void putAll(Map t) Object remove(Object key) int size() Collection values()

Page 70: The Container Store

© 2001 by Ashby M. Woolf Revision 3

System.out.println("Container a is an HashMap");a = new HashMap(); Builder.stuffItA(a);System.out.println("ksa is a Collection a.keySet()");Collection ksa = a.keySet();System.out.println("b is an HashMap");b = new HashMap(); Builder.stuffItB(b);System.out.println("ksb is a Collection b.keySet()");Collection ksb = b.keySet();System.out.println("a = " + a);System.out.println("ksa = " + ksa);System.out.println("b = " + b);System.out.println("ksb = " + ksb);System.out.println("ksa.removeAll(ksb);");ksa.removeAll( ksb );System.out.println("ksa = " + ksa);System.out.println("a = " + a);

Container a is an HashMapksa is a Collection a.keySet()Container b is an HashMapksb is a Collection b.keySet()a = {b=beta, a=alpha, g=gamma, d=delta}ksa = [b, a, g, d]b = {b=beta, 5=five, 4=four, d=delta}ksb = [b, 5, 4, d]ksa.removeAll(ksb);ksa = [a, g]a = {a=alpha, g=gamma}

keySet()

Page 71: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Collection

Set

List

AbstractCollection

AbstractListAbstractSet

SortedSet

AbstractSequentialList

HashMapTreeSetHashSetLinkedListArrayList TreeMapWeakHashMap

SortedMap

Map

AbstractMap

The Containers

void clear() boolean containsKey(Object key) boolean containsValue(Object value) Set entrySet() boolean equals(Object o) Object get(Object key) int hashCode() boolean isEmpty() Set keySet() Object put(Object key, Object value) void putAll(Map t) Object remove(Object key) int size() Collection values()

Map

HashMap TreeMapWeakHashMap

void clear() boolean containsKey(Object key) boolean containsValue(Object value) Set entrySet() boolean equals(Object o) Object get(Object key) int hashCode() boolean isEmpty() Set keySet() Object put(Object key, Object value) void putAll(Map t) Object remove(Object key) int size() Collection values()

Page 72: The Container Store

© 2001 by Ashby M. Woolf Revision 3

System.out.println("Container a is an HashMap");a = new HashMap(); Builder.stuffItA(a);System.out.println("ksa is a Collection a.keySet()");Collection ksa = a.keySet();System.out.println("b is an HashMap");b = new HashMap(); Builder.stuffItB(b);System.out.println("ksb is a Collection b.keySet()");Collection ksb = b.keySet();System.out.println("a = " + a);System.out.println("ksa = " + ksa);System.out.println("b = " + b);System.out.println("ksb = " + ksb);System.out.println("ksa.removeAll(ksb);");ksa.removeAll( ksb );System.out.println("ksa = " + ksa);System.out.println("a = " + a);

System.out.println("Container a is an HashMap");a = new HashMap(); Builder.stuffItA(a);System.out.println("vca is a Collection a.values()");Collection vca = a.values();System.out.println("b is an HashMap");b = new HashMap(); Builder.stuffItB(b);System.out.println("vca is a Collection b.values()");Collection vca = b.values();System.out.println("a = " + a);System.out.println("vca = " + vca);System.out.println("b = " + b);System.out.println("vcb = " + vcb);System.out.println("vca.removeAll(vcb);");vca.removeAll(vcb);System.out.println("vca = " + vca);System.out.println("a = " + a);

Container a is an HashMapvca is a Collection a.values()Container b is an HashMapvcb is a Collection b.values()a = {b=beta, a=alpha, g=gamma, d=delta}vca = [beta, alpha, gamma, delta]b = {b=beta, 5=five, 4=four, d=delta}vcb = [beta, five, four, delta]vca.removeAll(vcb);vca = [alpha, gamma]a = {a=alpha, g=gamma}

value()

Page 73: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Collection

Set

List

AbstractCollection

AbstractListAbstractSet

SortedSet

AbstractSequentialList

HashMapTreeSetHashSetLinkedListArrayList TreeMapWeakHashMap

SortedMap

Map

AbstractMap

The Containers

void clear() boolean containsKey(Object key) boolean containsValue(Object value) Set entrySet() boolean equals(Object o) Object get(Object key) int hashCode() boolean isEmpty() Set keySet() Object put(Object key, Object value) void putAll(Map t) Object remove(Object key) int size() Collection values()

Map

HashMap TreeMapWeakHashMap

void clear() boolean containsKey(Object key) boolean containsValue(Object value) Set entrySet() boolean equals(Object o) Object get(Object key) int hashCode() boolean isEmpty() Set keySet() Object put(Object key, Object value) void putAll(Map t) Object remove(Object key) int size() Collection values()

Page 74: The Container Store

© 2001 by Ashby M. Woolf Revision 3

System.out.println("Container a is an HashMap");a = new HashMap(); Builder.stuffItA(a);System.out.println("vca is a Collection a.values()");Collection vca = a.values();System.out.println("b is an HashMap");b = new HashMap(); Builder.stuffItB(b);System.out.println("vca is a Collection b.values()");Collection vca = b.values();System.out.println("a = " + a);System.out.println("vca = " + vca);System.out.println("b = " + b);System.out.println("vcb = " + vcb);System.out.println("vca.removeAll(vcb);");vca.removeAll(vcb);System.out.println("vca = " + vca);System.out.println("a = " + a);

System.out.println("Container a is an HashMap");a = new HashMap(); Builder.stuffItA(a);System.out.println("esa is a Collection a.entrySet()");Collection esa = a.entrySet();System.out.println("b is an HashMap");b = new HashMap(); Builder.stuffItB(b);System.out.println("esa is a Collection b.entrySet()");Collection esa = b.entrySet();System.out.println("a = " + a);System.out.println("esa = " + esa);System.out.println("b = " + b);System.out.println("esb = " + esb);System.out.println("esa.removeAll(esb);");esa.removeAll(esb);System.out.println("esa = " + esa);System.out.println("a = " + a);

Container a is an HashMapContainer esa is a Collection a.entrySet()Container b is an HashMapContainer esb is a Collection b.entrySet()a = {b=beta, a=alpha, g=gamma, d=delta}esa = [b=beta, a=alpha, g=gamma, d=delta]b = {b=beta, 5=five, 4=four, d=delta}esb = [b=beta, 5=five, 4=four, d=delta]esa.removeAll(esb);esa = [a=alpha, g=gamma]a = {a=alpha, g=gamma}

entrySet()

Page 75: The Container Store

© 2001 by Ashby M. Woolf Revision 3

The WeakHashMap

• Key in a HashMap are Objects

• If the only reference to a key Object is

from a WeakHashMap the Object can

be Garbage Collected

• The WeakHashMap entry for that key

will be removed.

Page 76: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Summary

• Arrays– Fast Simple– Type Specific

• Collections– Lists– Sets

• Maps– Key-Data Pairs

Page 77: The Container Store

© 2001 by Ashby M. Woolf Revision 3

Stuff to Add Next Time

• Multidimensional Arrays

• Cross Collection Operations

• ListIterator Example

• Iterate with a collection view of a Map

• Include examples of Class specific operations

Page 78: The Container Store

© 2001 by Ashby M. Woolf Revision 3

End of Content