Vous êtes sur la page 1sur 25

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


List list = new LinkedList(); for (int i = 1; i <= 10; i++) list.add("" + i); // Add 1 to 10 to list

Lists and Sets


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);

add is supported by most collections

Lists and Sets


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); for (int i = 0; i < list.size(); i++) System.out.println(list.get(i)); // Display contents of list

size is supported by all collections

Lists and Sets


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); 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

// Display contents of set

An iterator can be attached to any collection

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

Every collection has a constructor that expects an iterator.

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

Every collection has a constructor that expects a collection.

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

addAll is supported by most collections

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

removeAll is supported by most collections

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

retainAll is supported by most collections

The Collection Interface


boolean boolean void boolean boolean boolean int boolean Iterator boolean boolean boolean int Object[] Object[] add(Object o) addAll(Collection c) clear() contains(Object o) containsAll(Collection c) equals(Object o) hashCode() isEmpty() iterator() remove(Object o) removeAll(Collection c) retainAll(Collection c) size() toArray() toArray(Object[] a)

The Collection Interface


boolean boolean void boolean boolean boolean int boolean Iterator boolean boolean boolean int Object[] Object[] add(Object o) addAll(Collection c) clear() contains(Object o) containsAll(Collection c) equals(Object o) hashCode() isEmpty() iterator() remove(Object o) removeAll(Collection c) retainAll(Collection c) size() toArray() 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
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

List list2 = new LinkedList(tiny);

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


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

List list2 = new LinkedList(tiny);

Using the Method collectionView


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 // OK

List list2 = new LinkedList(tiny);

List list3 = new LinkedList(tiny.collectionView());

Using the Method collectionView


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 // Save a view

Collection view = tiny.collectionView(); list.addAll(view); view.addAll(list);

// Add tinys items to list // Add lists items to tiny

Using the Method collectionView


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 // Save a view

Collection view = tiny.collectionView(); list.addAll(view); view.addAll(list); view.removeAll(list);

// Add tinys items to list // Add lists items to tiny // Throws an exception because // TinyIterator does not support // remove

Vous aimerez peut-être aussi