# Java Collection Framework (JCF) JCF contient : * Interfaces * Implémentations * Algorithmes Avantages : * moins d'efforts * plus d'efficacité et de qualité * liens plus faciles entre différentes APIs * code plus réutilisable ```mermaid classDiagram Collection <|-- Set Collection <|-- List Collection <|-- Queue Set <|-- HashSet HashSet <|-- LinkedHashSet Set <|-- SortedSet SortedSet <|-- TreeSet List <|-- ArrayList List <|-- Stack List <|-- LinkedList Queue <|-- LinkedList Queue <|-- PriorityQueue Map <|-- SortedMap SortedMap <|-- TreeMap Map <|-- HashMap HashMap <|-- LinkedHashMap <> Collection <> Set <> List <> Queue <> SortedSet <> Map <> SortedMap ``` ## Interface [`Collection`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Collection.html) ```java interface Collection { // opérations de base int size(); boolean isEmpty(); boolean contains(Object o); boolean add(E e); // facultative boolean remove(Object o); // facultative // opérations de masse boolean containsAll(Collection c); boolean addAll(Collection c); // facultative boolean removeAll(Collection c); // facultative boolean retainAll(); // facultative void clear(); // facultative // opérations tableaux Object[] toArray(); T toArray(T[] a); // itérateur Iterator iterator(); } ``` Méthodes facultatives : `UnsupportedOperationException`. En plus de ces méthodes, chaque implémentation doit contenir un constructeur par défaut et un constructeur par recopie ```java public class MyCollection implements Collection { public MyCollection() { // crée une collection vide } public MyCollection(Collection c) { // crée une collection qui contient les mêmes éléments que c } } ``` ### Interface [`Iterator`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Iterator.html) ```java interface Iterator { boolean hasNext(); E next(); void remove(); // facultative } ``` Parcour typique d'une collection avec itérateur : ```java Collection trucs = ...; Iterator it = trucs.iterator(); while (it.hasNext()) { Truc truc = it.next(); // traiter truc } ``` La boucle foreach utilise un itérateur ! ```java for (Truc truc : trucs) { // traiter truc } ``` est juste un raccourci pour la boucle `while` précédente. Dans certains cas particuliers les implémentations *doivent* déclencher des exceptions. ```java Iterator it = trucs.iterator(); while(it.hasNext()) it.next(); it.next(); // NoSuchElementException ``` `remove()` supprime *l'élément renvoyé par le dernier appel de* `next()`. ```java Iterator it = trucs.iterator(); it.remove(); // IllegalStateException it = trucs.iterator(); Truc a = it.next(); Truc b = it.next(); it.remove(); // supprime b it.remove(); // IllegalStateException ``` Extrait de la documentation de `remove()` : > The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method, unless an overriding class has specified a concurrent modification policy. ```java Collection animaux = ...; // À ne JAMAIS faire Iterator it = animaux.iterator(); while (it.hasNext()) { Animal animal = it.next(); if (animal.estPoilu()) animaux.remove(animal); } // .. ni la forme équivalente for (Animal animal : animaux) { if (animal.estPoilu()) animaux.remove(animal); } // La bonne façon de faire : Iterator it = animaux.iterator(); while(it.hasNext()) { Animal animal = it.next(); if (animal.estPoilu()) it.remove(); } ``` ### Implémentation d'une collection *from skratch* TODO