Académique Documents
Professionnel Documents
Culture Documents
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
List
Set
Map
SortedSet
SortedMap
AbstractCollection
AbstractList
AbstractSequentialList
AbstractSet
AbstractMap
ArrayList
LinkedList
HashSet
TreeSet
HashMap
TreeMap
Set set = new HashSet(); // Add 5 random ints to set for (int i = 1; i <= 5; i++) set.add("" + (((int) (Math.random() * 10)) + 1);
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++) System.out.println(list.get(i)); // Display contents of list
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++) System.out.println(list.get(i)); Iterator iter = set.iterator(); while (iter.hasNext()) System.out.println(iter.next()); // Display contents of list
Transferring Data
List list = new LinkedList(); for (int i = 1; i <= 10; i++) list.add("" + i); Set set = new HashSet(list.iterator()); set // Add 1 to 10 to list
// Transfer to a new
Transferring Data
List list = new LinkedList(); for (int i = 1; i <= 10; i++) list.add("" + i); Set set = new HashSet(list); set // Add 1 to 10 to list
// Transfer to a new
Transferring Data
List list = new LinkedList(); for (int i = 1; i <= 10; i++) list.add("" + i); // Add 1 to 10 to list
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(); for (int i = 1; i <= 10; i++) list.add("" + i); // Add 1 to 10 to list
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(); for (int i = 1; i <= 10; i++) list.add("" + i); // Add 1 to 10 to list
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
List
Set
Map
Tiny
SortedSet
SortedMap
One-Way Transfers
List list = new LinkedList(); for (int i = 1; i <= 10; i++) list.add("" + i); Tiny tiny = new ArrayTiny(list); // Add 1 to 10 to list
// Allowed because List // extends Collection // 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.
collection-view object
Any class that implements collectionView will be compatible with the Collection interface without implementing that interface
// Add tinys items to list // Add lists items to tiny // Throws an exception because // TinyIterator does not support // remove