Bonjour à tous,
J'ai un problème avec la classe TreeMap. Je voulais utiliser cette classe pour son gros potentiel de tri mais le problème c'est qu'il lui faut un ordre total (pas de doublons dans les clés). J'ai donc bidouillé une classe Comparator afin de dépasser le problème mais du coup j'ai des erreurs.
Evidemment, si vous avez des idées afin d'avoir un Arbre qui gère un ordre avec doublon, je suis preneur. Un truc du style 0 <= 0 < 1 < 24 <= 24 < 25
Code de mon comparateur :
Code de ma clé
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 public class ComparateurCellNombreHost implements Comparator { public int compare(Object arg0, Object arg1) { if(!(arg0 instanceof CellNombreHost) && !(arg1 instanceof CellNombreHost)) { throw new ClassCastException(); } int i = ((CellNombreHost)arg0).getNb(); int j = ((CellNombreHost)arg1).getNb(); if(i == j) { return -1; } else { return (i - j) * 10; } } }
Code de mon utilisation de TreeMap
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 public class CellNombreHost { // VARIABLE private String host; private int nb; /** * Constructeur vide. */ public CellNombreHost() { super(); } /** * Constructeur avec paramètres. */ public CellNombreHost(String nomHost, int nombre) { super(); this.host = nomHost; this.nb = nombre; } /** * Renvoi la valeur du champ nb. * @return Retourne un int pour la valeur du champ nb */ public int getNb() { return nb; } /** * Modifie la valeur du champ nb. * @param nombre nombre de connexion mensuelle */ public void setNb(int nombre) { this.nb = nombre; } /** * Renvoi la valeur du champ host. */ public String getHost() { return host; } /** * Modifie la valeur du champ host. */ public void setHost(String nomHost) { this.host = nomHost; } }
Mon problème se situe au moment où je veux enlever de mon arbre la clé la plus haute. En fait, listCell.remove(listCell.lastKey()); ne fonctionne pas et donc très logiquement, j'ai une erreur de type JavaHeapSpace.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 TreeMap<CellNombreHost, String> listCell = new TreeMap<CellNombreHost, String>(new ComparateurCellNombreHost()); // Rempli un ARBRE avec des données for (int i = 0; i < v.size(); i++) { ArrayList work = (ArrayList) v.get(i); String name = (String) work.get(0); String nb = (String) work.get(1); URL url = new URL(name); if(ancienUrl != null && (!ancienUrl.getHost().equals(url.getHost())) && new Integer(nb) > nbTolereSiteBloque && i != 0) { listCell.put(new CellNombreHost(ancienUrl.getHost(), new Integer(nb)), ancienUrl.getHost()); nbUrl = 0; } nbUrl++; ancienUrl = url; } while(listCell.size() != 0) { CellNombreHost key = listCell.lastKey(); ch.append("<tr><td>"); ch.append(key.getHost()); ch.append("</td><td>"); ch.append(key.getNb()); ch.append("</td></tr>"); if(listCell.containsKey(key)) { System.out.println("ok"); } if(listCell.containsKey(listCell.lastKey())) { System.out.println("ok"); } listCell.remove(listCell.lastKey()); } ch.append("</table></body></html>");
Partager