Working with Java Collections
Java 2 Collections Lists - linear, supporting many operations Sets - unordered, unique items Sorted sets - like sets, but items can be visited in sorted order
Java 2 Collections Maps - unordered, items accessed by keys Sorted maps - same as maps, but items can be visited in sorted order
Java 2 Collection Interfaces Collection List Set Map SortedSet SortedMap
Java 2 Collection Classes Object AbstractCollection AbstractList AbstractSequentialList AbstractSet AbstractMap ArrayList LinkedList HashSet TreeSet HashMap TreeMap
Lists and Sets
Lists and Sets Set set = new HashSet(); // Add 5 random ints to set for (int i = 1; i <= 5; i++) set.add("" + (((int) (Math.random() * 10)) + 1); add is supported by most collections
Lists and Sets Set set = new HashSet(); // Add 5 random ints to set for (int i = 1; i <= 5; i++) set.add("" + (((int) (Math.random() * 10)) + 1); for (int i = 0; i < list.size(); i++) // Display contents of list System.out.println(list.get(i)); size is supported by all collections
Lists and Sets Set set = new HashSet(); // Add 5 random ints to set for (int i = 1; i <= 5; i++) set.add("" + (((int) (Math.random() * 10)) + 1); for (int i = 0; i < list.size(); i++) // Display contents of list System.out.println(list.get(i)); Iterator iter = set.iterator(); // Display contents of set while (iter.hasnext()) System.out.println(iter.next()); An iterator can be attached to any collection
Transferring Data Set set = new HashSet(list.iterator()); // Transfer to a new set Every collection has a constructor that expects an iterator.
Transferring Data Set set = new HashSet(list); // Transfer to a new set Every collection has a constructor that expects a collection.
Transferring Data Set set = new HashSet(); // Add 5 random ints to set for (int i = 1; i <= 5; i++) set.add("" + (((int) (Math.random() * 10)) + 1); list.addall(set); // Add all data in set // to list addall is supported by most collections
Filtering Data Set set = new HashSet(); // Add 5 random ints to set for (int i = 1; i <= 5; i++) set.add("" + (((int) (Math.random() * 10)) + 1); list.removeall(set); // Remove all data from list // that are in set removeall is supported by most collections
Filtering Data Set set = new HashSet(); // Add 5 random ints to set for (int i = 1; i <= 5; i++) set.add("" + (((int) (Math.random() * 10)) + 1); list.retainall(set); // Remove all data from list // that are not in set retainall is supported by most collections
The Collection Interface boolean add(object o) boolean addall(collection c) void clear() boolean contains(object o) boolean containsall(collection c) boolean equals(object o) int hashcode() boolean isempty() Iterator iterator() boolean remove(object o) boolean removeall(collection c) boolean retainall(collection c) int size() Object[] toarray() Object[] toarray(object[] a)
The Collection Interface boolean add(object o) boolean addall(collection c) void clear() boolean contains(object o) boolean containsall(collection c) boolean equals(object o) int hashcode() boolean isempty() Iterator iterator() boolean remove(object o) boolean removeall(collection c) boolean retainall(collection c) int size() Object[] toarray() Object[] toarray(object[] a) The mutators (in green) are optional operations
Not All Collections Implement Collection Collection List Set Map Tiny SortedSet SortedMap
One-Way Transfers Tiny tiny = new ArrayTiny(list); // Allowed because List // extends Collection List list2 = new LinkedList(tiny); // Not allowed because Tiny // does not extend Collection
Collection-View A collection-view is an object that implements the Collection interface has a collection as a backing store which does not implement the collection interface allows clients to use many of the Collection methods with such collections as maps, tinys, etc.
A Collection-View Is Similar to an Iterator backing store collection iterator object client using hasnext, next backing store collection collection-view object client using removeall, retainall, etc. A collection-view allows an object to masquerade as a collection
The Method collectionview Iterator iterator() Collection collectionview() // Returns an iterator // Returns a collection-view Any class that implements collectionview will be compatible with the Collection interface without implementing that interface
Using the Method collectionview Tiny tiny = new ArrayTiny(list); // Allowed because List // extends Collection List list2 = new LinkedList(tiny); // Not allowed because Tiny // does not extend Collection
Using the Method collectionview Tiny tiny = new ArrayTiny(list); // Allowed because List // extends Collection List list2 = new LinkedList(tiny); // Not allowed because Tiny // does not extend Collection List list3 = new LinkedList(tiny.collectionView()); // OK
Using the Method collectionview Tiny tiny = new ArrayTiny(list); // Allowed because List // extends Collection Collection view = tiny.collectionview(); // Save a view list.addall(view); view.addall(list); // Add tiny s items to list // Add list s items to tiny
Using the Method collectionview Tiny tiny = new ArrayTiny(list); // Allowed because List // extends Collection Collection view = tiny.collectionview(); // Save a view list.addall(view); view.addall(list); view.removeall(list); // Add tiny s items to list // Add list s items to tiny // Throws an exception because // TinyIterator does not support // remove