Java Collection
Java Collection
// Opérations de groupe
boolean containsAll(Collection<?> c);
boolean addAll(Collection<? extends E> c);
boolean removeAll(Collection<?> c);
boolean retainAll(Collection<?> c);
void clear();
// Transformations en tableaux
Object[] toArray();
<T> T[] toArray(T a[]);
}
1
public interface Iterator<E> {
boolean hasNext();
<E> next();
void remove();
}
boolean hasPrevious();
E previous();
int nextIndex();
int previousIndex();
void remove();
void set(E o);
void add(E o);
}
2
List
// recherche
int indexOf(Object o);
int lastIndexOf(Object o);
// Itération
ListIterator<E> listIterator();
ListIterator<E> listIterator(int index);
Exemples de création
List<String> l = new ArrayList<String>();
List<Integer> l = new LinkedList<Integer>();
3
Set
Exemple de création
Set<String> s = new HashSet<String>();
4
Map
Création
Map<String,Integer> m = new HashMap<String,Integer>();
Map<String,Integer> m = new LinkedHashMap<String,Integer>();
5
SortedSet
// points extrêmes
E first();
E last();
// accès au comparateur
Comparator<? super E> comparator();
}
Création
SortedSet<String> ss = new TreeSet<String>();
6
SortedMap
// accès au comparateur
Comparator<? super K> comparator();
}
Création
SortedMap<String,Integer> sm = new TreeMap<String,Integer>();
7
Collections
Contient des méthodes statiques qui opèrent et retournent des collections. On ne
présente ici que les méthodes qui nous semblent les plus couramment utilisées.
// Tri
public static <T extends Comparable<? super T>>
void sort(List<T> list){…}
public static <T> void sort(List<T> list,
Comparator<? Super T> c){…}
// Recherche
public static <T> int binarySearch (
List<? extends Comparable<? super T>>list, T key){…}
public static <T> int binarySearch(
List<? extends T> list, T key,
Comparator<? super T> c){…}
// Remplissage
public static <T>void fill(List<? super T> list, T o){ …}
public static <T>void copy (List<? super T> dest,
List<? extends T> src){…}
// Brassage
public static void reverse(List<?> l) { ... }
public static void shuffle(List<?> list) { ... }
public static void shuffle(List<?> list, Random rnd){…}
8
Arrays
Algorithmes génériques sur les tableaux dont les éléments sont les types prédéfinis (long,
int, short, char, byte, float, double) ainsi que Object. Dans la description suivante, ces
types sont notés T
// Tri
public static void sort( int[] a) { ... }
public static void sort( int[] a, int fromIndex,
int toIndex) {…}
// idem pour boolean, char, double, long, short, float
public static <T>void sort(T[] a,
Comparator<? super T> c){…}
public static <T>void sort(T[] a,
int fromIndex, int toIndex,
Comparator<? super T> c){…}
// Recherche
public static int binarySearch(int[] a, int key) { ... }
// idem pour byte, char, double, long, short, float
public static <T>int binarySearch(T[] a, T key,
Comparator<? Super T> c){…}
// Test d'égalité de tous les éléments des tableaux
public static boolean equals(int[] a, int[] a2) { ... }
// Remplissage
public static void fill( int[] a, int[] val){…}
public static void fill( int[] a,
int fromIndex, int toIndex,
int val) { ... }
}