[Débutant] LinkedHashSet comment le trier
On ma deja pas mal aider au sujet des List , Set ect , et je penser etre au top , mais c'est pas le cas.
J'ai utiliser une LinkedHashSet , pour stocker des Strings a partir de la ligne de commandes , et je doit a présent les trié par ordres alphabétique.
Ayant eu as faire pareil sur un Arraylist , j'ai repris l'aide que l'on m'avez fourni , en implementant Comparable , et en redefinissant compareTo().
Malheureusement sa marche pas , je copie le code:
Code:
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 55
|
import java.util.*;
public class stockAset implements Comparable{
static LinkedHashSet hs = new LinkedHashSet();
public static void main(String[] args){
for(int i=0;i<args.length;i++){
if(contientA(args[i])){
hs.add(args[i]);
}
}
Collections.sort(hs);
lectureByIt(hs);
}
public static boolean contientA(String tmp){
boolean test=false;
char valeur = 'a';
int nbr = tmp.length();
for(int y=0;y<nbr;y++){
if(tmp.charAt(y)== valeur){
test=true;
break;
}
else{
test=false;
}
}
return test;
}
public int compareTo(Object a){
int result=0;
String tmpa,tmpb;
char ca[],cb[];
tmpa = this.toString();
tmpb = (String)a;
ca = tmpa.toCharArray();
cb = tmpb.toCharArray();
if(ca[0] > cb[0]){
result = -1;
}
if(ca[0] < cb[0]){
result = 1;
}
if(ca[0] == cb[0]){
result = 0;
}
return result;
}
public static void lectureByIt(LinkedHashSet itmp){
System.out.println("Affichage par Iterateur");
Iterator it = itmp.iterator();
while(it.hasNext()){
System.out.println(it.next().toString());
}
}
} |
L'erreur est :
Code:
1 2 3 4 5 6 7
|
stockAset.java:10: cannot resolve symbol
symbol : method sort (java.util.LinkedHashSet)
location: class java.util.Collections
Collections.sort(hs);
^
1 error |