the container store
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 PresentationTRANSCRIPT
© 2001 by Ashby M. Woolf Revision 3
The Container Store
A Place for Everything and Everything in its Place
© 2001 by Ashby M. Woolf Revision 3
Containers
Arrays,
Collections and
Maps
© 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.
© 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); ^
© 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; ^
© 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
© 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
© 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
© 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.
© 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); ^
© 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; ^
© 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
© 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(); • • •}
© 2001 by Ashby M. Woolf Revision 3
Array Utilitiesjava.util.Arrays
© 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.
© 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
© 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))
© 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).
© 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)
© 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
© 2001 by Ashby M. Woolf Revision 3
Array Summary
• Size determined at creation
• Type specific
• Uses TypeName[ ] syntax
• Fast
© 2001 by Ashby M. Woolf Revision 3
Arrays
A Change of Topic
Introductionto
Containers
© 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; }}
© 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"); } }}
© 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
© 2001 by Ashby M. Woolf Revision 3
Collection
Set
List
AbstractCollection
AbstractListAbstractSet
SortedSet
AbstractSequentialList
HashMapTreeSetHashSetLinkedListArrayList TreeMapWeakHashMap
SortedMap
Map
AbstractMap
The Containers
TreeSetHashSetLinkedListArrayList
© 2001 by Ashby M. Woolf Revision 3
Introductionto
Containers
A Change of Topic
Collections
© 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
© 2001 by Ashby M. Woolf Revision 3
Data Structure Reference
http://swww.ee.uwa.edu.au/~plsd210/ds/
© 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();
© 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;
}}
© 2001 by Ashby M. Woolf Revision 3
Collection
Set
List
AbstractCollection
AbstractListAbstractSet
SortedSet
AbstractSequentialList
HashMapTreeSetHashSetLinkedListArrayList TreeMapWeakHashMap
SortedMap
Map
AbstractMap
The ContainersCollection
TreeSetHashSetLinkedListArrayList
© 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;
}}
© 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.
© 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]
© 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
© 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
© 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
© 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
© 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
© 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
© 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
© 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
© 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
© 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
© 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
© 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
© 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
© 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
© 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
© 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
© 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
© 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]
© 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
© 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).
© 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
© 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
© 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).
© 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.
© 2001 by Ashby M. Woolf Revision 3
Collection
Set
List
AbstractCollection
AbstractListAbstractSet
SortedSet
AbstractSequentialList
HashMapTreeSetHashSetLinkedListArrayList TreeMapWeakHashMap
SortedMap
Map
AbstractMap
The Containers
HashMap TreeMap
© 2001 by Ashby M. Woolf Revision 3
Collections
A Change of Topic
Maps
© 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
© 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()
© 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;
}}
© 2001 by Ashby M. Woolf Revision 3
Collection
Set
List
AbstractCollection
AbstractListAbstractSet
SortedSet
AbstractSequentialList
HashMapTreeSetHashSetLinkedListArrayList TreeMapWeakHashMap
SortedMap
Map
AbstractMap
The ContainersMap
HashMap TreeMapWeakHashMap
© 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()
© 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
© 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
© 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()
© 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()
© 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()
© 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()
© 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()
© 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()
© 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.
© 2001 by Ashby M. Woolf Revision 3
Summary
• Arrays– Fast Simple– Type Specific
• Collections– Lists– Sets
• Maps– Key-Data Pairs
© 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
© 2001 by Ashby M. Woolf Revision 3
End of Content