Année académique 2020-2021
Collections, Stream, Lambda &
Calcul parallélisé
Ir Olivier DEBAUCHE
Prof Pierre Manneback
Adriano GUTTADAURIA
Les listes: class LinkedList Les vecteurs: class ArrayList
Les queues: interface Queue (FiFo)
Les queues à double entrée: interface Deque (LiFo) Les ensembles organisés par hachage: class HashSet Les ensembles organisés en arbre binaire: class TreeSet Table de hachage: classe HashMap
Arbre binaire: classe TreeMap
Collections
En C++
#include<list>
#include<string>
list<string> ma_liste;
list<string>::iterator it;
for (it = ma_liste.begin(); it !=
ma_liste.end(); it++) { … }
Collections (LinkedList)
En Java
import java.util.*;
LinkedList<String> ma_liste = new LinkedList<String>();
ListIterator<String> it = ma_liste.listIterator();
while (it.hasNext()) {
… }
En C++
for (it=ma_liste.end();
it!=ma_liste.begin(); it--) { … }
it++;
it--;
Collections : remarques
En Java while(it.previous()) {
… }
it.next();
it.previous();
En C++
#include<list>
#include<string>
vector<string> mon_vec;
vector<string>::iterator it;
for (it = mon_vec.begin(); it !=
mon_vec.end(); it++) { … }
Collections (ArrayList)
En Java
import java.util.*;
ArrayList<String> mon_vec = new ArrayList<String>();
for(int i=0; i<mon_vec.size();
i++) { … }
Collections (Piles et Files)
Queue
.add() ajoute un élément.
.poll() prélève et supprime l’élément de la queue.
.peek() prélève sans supprimer un élément de la queue.
Note: LinkedList implémente Queue.
Deque
.addFirst(e) ajoute un élément en début de file.
.addLast(e) ajoute un élément en fin de file.
.pollFirst() prélève un supprime un élément au début.
.pollLast() prélève un supprime un élément à la fin.
.peekFirst() prélève un élément au début de la file.
.peekLast() prélève un élément à la fin de la file.
Note: LinkedList implémente
Deque.
HashSet
Nécessite la définition des méthodes hashCode() et equals() dans la classe
Mais nécessite la connaissance statistique des données pour choisir un algorithme de
hashCode.
Collections (Les ensembles)
TreeSet
Nécessite l’implémentation de l’interface Comparable et la définition de la méthode compareTo.
Effectue un tri complet des données.
Un ensemble contient des valeurs uniques
HashSet (Exemple)
Import java.util.*;
public class hashsetdemo {
public static main(String args[]) {
Animal a1 = new Animal(‘’Cochon’’, 120);
Animal a2 = new Animal(‘’Martre’’, 1.3);
Animal a3 = new Animal(‘’Oiseau’’, 0.3);
HashSet<Animal> ens = new HashSet<Animal>();
ens.add(a1); ens.add(a2); ens.add(a3);
affiche(ens);
}
public static void affiche (HashSet<Animal> ens) { Iterator<Animal> it = ens.iterator();
while (it.hasNext()) { Animal a = it.next();
a.affiche();
} } }
Collections (Les ensembles)
class Animal {
private String nom;
private float poids;
Animal (String nom, float poids) { this.nom = nom;
this.poids = poids;
}
public int hashCode() {
return nom.length() + poids;
}
public booleanequals(Object ani) { Animal a = (Animal) ani;
return ((this.nom == a.nom) & (this.poids = a.poids) ;
}
public void affiche() {
System.out.print(‘’[‘’ + nom + ‘’ ‘’ + poids + ‘’]’’);
} }
TreeSet (Exemple)
Import java.util.*;
public class treesetdemo {
public static main(String args[]) {
Animal a1 = new Animal(‘’Cochon’’, 120);
Animal a2 = new Animal(‘’Martre’’, 1.3);
Animal a3 = new Animal(‘’Oiseau’’, 0.3);
TreeSet<Animal> ens = new TreeSet<Animal>();
ens.add(a1); ens.add(a2); ens.add(a3);
affiche(ens);
}
public static void affiche (HashSet<Animal> ens) { Iterator<Animal> it = ens.iterator();
while (it.hasNext()) { Animal a = it.next();
a.affiche();
} } }
Collections (Les ensembles)
class Animal implements Comparable { private String nom;
private float poids;
Animal (String nom, float poids) { this.nom = nom;
this.poids = poids;
}
public int compareTo(Object ani) { Animal a = (Animal) ani;
if (this.nom < a.nom) return -1;
else if (this.nom > a.nom) return 1;
else if (this.poids < a.poids) return -1;
else if (this.poids > a.poids) return 1;
else return 0;
}
public void affiche() {
System.out.print(‘’[‘’ + nom + ‘’ ‘’ + poids + ‘’]’’);
} }
HashMap
HashMap<K,V> m = new HashMap<K,V>();
TreeMap
TreeMap<K,V> m = new TreeMap<K,V>();
Exemple:
HashMap<String,Int> m = new HashMap<String,Int>();
m.put(‘’c’’, 10); // Ajoute ou modifie un élément String ch = m.get(‘’c’’); // Récupère un élément
Collection <Int> = valeurs = m.values(); // Récupère les valeurs Set<String> cles = m.keySet(); Récupère les clés
m.remove(‘’c’’); // Supprime l’élément de clé ‘’c’’
Collections (Les tableaux associatifs)
HashMap = table de hachage / TreeMap = arbre binaire
Exemple (suite):
HashMap<String,Int> m = new HashMap<String,Int>();
Set<Map.Entry<String, Int>> entrees = m.entrySet();
Iterator<Map.Entry<String,Int>> it = entrees.iterator();
while (it.hasNext()) {
Map.Entry<String, Int> entree = it.next();
String valeur = entree.getValue();
if (valeur.equals(‘’20’’) {
System.out.println(‘’Trouvée à la clé: ‘’ + entree.getKey());
} }