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
Java 2 Collection Interfaces
Collection
List Set Map
Java 2 Collection Classes
AbstractCollection
AbstractList AbstractSet AbstractMap
HashSet HashMap Object
AbstractSequentialList
TreeSet TreeMap ArrayList LinkedList
Lists and Sets
List list = new LinkedList(); // Add 1 to 10 to list for (int i = 1; i <= 10; i++)
Lists and Sets
List list = new LinkedList(); // Add 1 to 10 to list for (int i = 1; i <= 10; i++)
list.add("" + i);
Set set = new HashSet(); // Add 5 random ints to set for (int i = 1; i <= 5; i++)
set.add("" + (((int) (Math.random() * 10)) + 1);
Lists and Sets
List list = new LinkedList(); // Add 1 to 10 to list for (int i = 1; i <= 10; i++)
list.add("" + i);
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));
Lists and Sets
List list = new LinkedList(); // Add 1 to 10 to list for (int i = 1; i <= 10; i++)
list.add("" + i);
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());
Transferring Data
List list = new LinkedList(); // Add 1 to 10 to list for (int i = 1; i <= 10; i++)
list.add("" + i);
Set set = new HashSet(list.iterator()); // Transfer to a new set
Transferring Data
List list = new LinkedList(); // Add 1 to 10 to list for (int i = 1; i <= 10; i++)
list.add("" + i);
Set set = new HashSet(list); // Transfer to a new set
Transferring Data
List list = new LinkedList(); // Add 1 to 10 to list for (int i = 1; i <= 10; i++)
list.add("" + i);
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
Filtering Data
List list = new LinkedList(); // Add 1 to 10 to list for (int i = 1; i <= 10; i++)
list.add("" + i);
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
Filtering Data
List list = new LinkedList(); // Add 1 to 10 to list for (int i = 1; i <= 10; i++)
list.add("" + i);
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
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)Not All Collections
Implement
Collection
Collection
List Set Map
SortedSet SortedMap
One-Way Transfers
List list = new LinkedList(); // Add 1 to 10 to list for (int i = 1; i <= 10; i++)
list.add("" + i);
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
A Collection-View Is
Similar to an Iterator
backing store
collection
backing store
collection
iterator
object
collection-view
object
client using
hasNext
,
next
client using
removeAll
,
retainAll
,
etc.
A collection-view allows an object to masquerade
as a collection
The Method
collectionView
Iterator iterator() // Returns an iterator
Collection collectionView() // Returns a collection-view
Any class that implements
collectionView
will be
compatible with the
Collection
interface without
implementing that interface
Using the Method
collectionView
List list = new LinkedList(); // Add 1 to 10 to list for (int i = 1; i <= 10; i++)
list.add("" + i);
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
List list = new LinkedList(); // Add 1 to 10 to list for (int i = 1; i <= 10; i++)
list.add("" + i);
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
List list = new LinkedList(); // Add 1 to 10 to list for (int i = 1; i <= 10; i++)
list.add("" + i);
Tiny tiny = new ArrayTiny(list); // Allowed because List // extends Collection Collection view = tiny.collectionView(); // Save a view
list.addAll(view); // Add tiny’s items to list view.addAll(list); // Add list’s items to tiny
Using the Method
collectionView
List list = new LinkedList(); // Add 1 to 10 to list for (int i = 1; i <= 10; i++)
list.add("" + i);
Tiny tiny = new ArrayTiny(list); // Allowed because List // extends Collection Collection view = tiny.collectionView(); // Save a view
list.addAll(view); // Add tiny’s items to list view.addAll(list); // Add list’s items to tiny view.removeAll(list); // Throws an exception because
// TinyIterator does not support // remove