790 likes | 963 Views
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 };
E N D
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 }; 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.
Arrays of Primitives public 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); ^
Arrays of Primitives public 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; ^
Arrays of Primitives public 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 = 3 1 2 3 c.length = 5 0 10 20 30 40
Arrays Verses the other Containers • Most Efficient • Type Specific • Size Fixed at Creation • Bounds Checking • Other Containers Don't Hold Primitives Directly
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
Arrays of Objects 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 initialized to null.
Arrays of Objects 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; } } variable a might not have been initialized System.out.println("a.length = " + a.length); ^
Arrays of Objects 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; } } cannot assign a value to final variable length c.length = 3; ^
Arrays of Objects 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; } } b.length = 3 one two three c.length = 5 Value = 0 Value = 10 Value = 20 Value = 30 Value = 40
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(); • • • }
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.
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
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))
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).
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)
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
Array Summary • Size determined at creation • Type specific • Uses TypeName[ ] syntax • Fast
A Change of Topic Arrays Introduction to Containers
A Review - Interfaces, Abstract Classes, and Classes interface Pet { String name; void speak(int i); void setName(String s) { name = s; } String getName() { return name; } } 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"); } } }
A Review - Interfaces, Abstract Classes, and Classes interface Pet { void speak(int i); void setName(String s); String getName(); } interface Pet { void speak(int i); void setName(String s) { name = s; } String getName() { return name; } } interface Pet { void speak(int i); void setName(String s); String getName() { return name; } } interface Pet { String name; 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; } public String getName() { return name; } } abstract class APet implements Pet{ String name; public void setName(String s) { name = s; } } abstract class APet implements Pet{ String name; public void setName(String s) { name = s; } public String getName() { return name; } } abstract class APet implements Pet{ } abstract class APet implements Pet{ String name; } class Bird extends Pet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Tweet"); } } } class Bird extends APet { public void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Tweet"); } } } class Bird extends APet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Tweet"); } } } class Bird extends APet { public 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"); } } } class Dog extends APet { public void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Woof"); } } } class Dog extends APet { void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Woof"); } } } class Dog extends APet { public void speak(int i) { for(int k=0; k<i; k++) { System.out.println("Woof"); } } }
Collection Map Set List SortedMap SortedSet AbstractCollection AbstractList AbstractSet AbstractMap AbstractSequentialList HashSet TreeSet WeakHashMap HashMap ArrayList LinkedList TreeMap The Containers Collection Map Set List SortedMap SortedSet AbstractCollection AbstractList AbstractSet AbstractMap AbstractSequentialList HashSet TreeSet WeakHashMap HashMap ArrayList LinkedList TreeMap
HashSet TreeSet ArrayList LinkedList The Containers Collection Map Set List SortedMap SortedSet AbstractCollection AbstractList AbstractSet AbstractMap AbstractSequentialList HashSet TreeSet WeakHashMap HashMap ArrayList LinkedList TreeMap
A Change of Topic Introduction to Containers Collections
Object Object Object Object Object Object • • • 0 Object 1 2 Object Object F B 3 4 Object F null 5 Object 6 Object Object B null • Object • • Object Object Object Object Object L F L L R B R R n Object Object Object null null null null null null Object null null The Collections ArrayList HashSet 1 LinkedList 0 1 0 0 1 1 TreeSet 1 0 1 0
Data Structure Reference http://swww.ee.uwa.edu.au/~plsd210/ds/
The Containers Collection Map Set 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(); List SortedMap SortedSet AbstractCollection AbstractList AbstractSet AbstractMap AbstractSequentialList HashSet TreeSet WeakHashMap HashMap ArrayList LinkedList TreeMap
A Utility for Filling Collections class 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; } }
HashSet TreeSet ArrayList LinkedList The Containers Collection Collection Map Set List SortedMap SortedSet AbstractCollection AbstractList AbstractSet AbstractMap AbstractSequentialList HashSet TreeSet WeakHashMap HashMap ArrayList LinkedList TreeMap
A Utility for Filling Collections class 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; } }
Demonstrating the Collections public 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 * * * * * } } Hiding the declarations to make space. Container a is an ArrayList Container b is a ArrayList a = [alpha, gamma, delta, beta] b = [beta, four, five, delta]
Demonstrating the Collections public 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 ArrayList Container b is a ArrayList a = [alpha, gamma, delta, beta] b = [beta, four, five, delta]
The Containers Collection Collection Map addAll()defined in the Collections interface. Set List SortedMap SortedSet AbstractCollection AbstractList AbstractSet AbstractMap AbstractSequentialList HashSet TreeSet WeakHashMap HashMap ArrayList LinkedList HashSet TreeSet TreeMap ArrayList LinkedList
ArrayList Object Object Object • • • Demonstrating addAll() with ArrayList 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); } } 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]
LinkedList Object F Object B null null Object F B Demonstrating addAll() with LinkedList 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); } } 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 LinkedList Container b is a LinkedList a = [alpha, gamma, delta, beta] b = [beta, four, five, delta] a = [alpha, gamma, delta, beta, beta, four, five, delta]
TreeSet Object L R Object Object null null null null Demonstrating addAll() with TreeSet 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); } } 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 TreeSet Container 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.
HashSet 0 1 Object 1 0 2 Object 1 • • Object 1 • 0 n Object 1 0 Demonstrating addAll() with HashSet 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); } } 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 HashSet Container b is a HashSet a = [gamma, beta, alpha, delta] b = [five, four, beta, delta] a = [gamma, five, four, beta, alpha, delta] No Duplicates.
The Containers Collection Collection Map removeAll()in the Collections interface. Set List SortedMap SortedSet AbstractCollection AbstractList AbstractSet AbstractMap AbstractSequentialList HashSet TreeSet WeakHashMap HashMap ArrayList LinkedList HashSet TreeSet TreeMap ArrayList LinkedList
ArrayList Object Object Object • • • Demonstrating removeAll() with ArrayList 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); } } 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]
LinkedList Object F Object B null null Object F B Demonstrating removeAll() with LinkedList 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); } } 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 LinkedList Container b is an LinkedList a = [alpha, gamma, delta, beta] b = [beta, four, five, delta] a = [alpha, gamma]
TreeSet Object L R Object Object null null null null Demonstrating removeAll() with TreeSet 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); } } 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 TreeSet Container b is an TreeSet a = [alpha, beta, delta, gamma] b = [beta, delta, five, four] a = [alpha, gamma] Sorted and No Duplicates.
HashSet 0 1 Object 1 0 2 Object 1 • • Object 1 • 0 n Object 1 0 Demonstrating removeAll() with HashSet 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); } } 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 HashSet Container b is an HashSet a = [gamma, beta, alpha, delta] b = [five, four, beta, delta] a = [gamma, alpha] No Duplicates.
ArrayList Object Object Object • • • Demonstrating retainAll() with ArrayList 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); } } 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]
LinkedList Object F Object B null null Object F B Demonstrating retainAll() with LinkedList 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); } } 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 LinkedList Container b is an LinkedList a = [alpha, gamma, delta, beta] b = [beta, four, five, delta] a = [delta, beta]
TreeSet Object L R Object Object null null null null Demonstrating retainAll() with TreeSet 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); } } 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 TreeSet Container b is an TreeSet a = [alpha, beta, delta, gamma] b = [beta, delta, five, four] a = [beta, delta] Sorted and No Duplicates.
HashSet 0 1 Object 1 0 2 Object 1 • • Object 1 • 0 n Object 1 0 Demonstrating retainAll() with HashSet 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); } } 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 HashSet Container b is an HashSet a = [gamma, beta, alpha, delta] b = [five, four, beta, delta] a = [beta, delta] No Duplicates.
Working with a Single Container 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); } } 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("a = " + a); } } Demo stuff goes here Container a is an ArrayList a = [alpha, gamma, delta, beta]