Bonjour,
Je voudrais créer une Queue dont les éléments sont ordonnés par rapport à leur priorité. J'ai créé un comparateur d'entiers tout simple mais celui-ci n'ordonne pas correctement ma liste.
Avec le comparateur et le code test ci-dessous j'obtiens pour résultat 1, 0, 1 au lieu de 1, 1, 0.
Est-ce qu'il existe un bug connu pour l'utilisation de la classe Comparator ou bien est-ce que j'ai mal codé ? (sans l'utilisation de ma classe Comparator l'ordonnancement est bon ...)
Voici mon Comparateur :
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 public class NewComp implements Comparator<Integer> { /** * default constructor */ public NewComp() { } /** * Compare the priority level of two Integer * elements in argument. * * @return <pre> * 1 if this priority level is lower * -1 if this priority level is higher * 0 if the priority levels are equals * </pre> {@inheritDoc} */ public int compare(Integer app1, Integer app2) { if (app1.intValue() > app2.intValue()) { return -1; } else if (app1.intValue() < app2.intValue()) { return 1; } else { return 0; } } public boolean equals(Integer app1, Integer app2) { boolean result = false; if (app1.intValue() == app2.intValue()) { result = true; } return result; } }
Voici ma classe test :
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 public class TestComparator extends PriorityBlockingQueue<Integer>{ public TestComparator(int capa, Comparator<Integer> comp) { //super(); super(capa, comp); } /** * default constructor */ public static void main (String[] args) { TestComparator test = new TestComparator(1, new NewComp()); test.offer(new Integer(0)); test.offer(new Integer(1)); test.offer(new Integer(1)); System.out.println("end"); //Empty } }
Merci par avance pour votre aide,
Eva
Partager