Tri de map sur les valeur
Résolu
omidaoui
Messages postés
32
Date d'inscription
Statut
Membre
Dernière intervention
-
omidaoui Messages postés 32 Date d'inscription Statut Membre Dernière intervention -
omidaoui Messages postés 32 Date d'inscription Statut Membre Dernière intervention -
bonjour
j'ai essayer de trier une map <String, Double> sur ses valeur double et j'ai eu un probleme du fait ke s'il y a deux Key qui ont deux value identique il seront eliminer du sortedSet crée, alors mon Map trié resultante ne contien plus les les key ki ont ces value identique
aider moi svp
voila ma methode trierMap
mon proble reside ici
Set<Double> sortedSet = new TreeSet<Double>(valueList);
sortedSet n'accepte pas les doublons
et ici j'ai inverser le parcours des indexe pour trier d'une façon décroissante :
au lieu de :
map.put(keyList.get(valueList.indexOf(sortedArray[i])), (Double) sortedArray[i]);
j'ai fait :
map.put(keyList.get(valueList.indexOf(sortedArray[size-i-1])), (Double) sortedArray[size-i-1]);
est ce ke c'est faisable ou est ce k il y a une facon plus correcte
voila ma methode :
public Map<String,Double> TrierMap(Map<String,Double> mapp){
Map<String, Double> map = new LinkedHashMap<String, Double>();
List<String> keyList = new ArrayList<String>(mapp.keySet());
List<Double> valueList = new ArrayList<Double>(mapp.values());
Set<Double> sortedSet = new TreeSet<Double>(valueList);
Object[] sortedArray = sortedSet.toArray();
int size = sortedArray.length;
for (int i = 0; i < size; i++) {
map.put(keyList.get(valueList.indexOf(sortedArray[size-i-1])), (Double) sortedArray[size-i-1]);
}
return map;
}
j'ai essayer de trier une map <String, Double> sur ses valeur double et j'ai eu un probleme du fait ke s'il y a deux Key qui ont deux value identique il seront eliminer du sortedSet crée, alors mon Map trié resultante ne contien plus les les key ki ont ces value identique
aider moi svp
voila ma methode trierMap
mon proble reside ici
Set<Double> sortedSet = new TreeSet<Double>(valueList);
sortedSet n'accepte pas les doublons
et ici j'ai inverser le parcours des indexe pour trier d'une façon décroissante :
au lieu de :
map.put(keyList.get(valueList.indexOf(sortedArray[i])), (Double) sortedArray[i]);
j'ai fait :
map.put(keyList.get(valueList.indexOf(sortedArray[size-i-1])), (Double) sortedArray[size-i-1]);
est ce ke c'est faisable ou est ce k il y a une facon plus correcte
voila ma methode :
public Map<String,Double> TrierMap(Map<String,Double> mapp){
Map<String, Double> map = new LinkedHashMap<String, Double>();
List<String> keyList = new ArrayList<String>(mapp.keySet());
List<Double> valueList = new ArrayList<Double>(mapp.values());
Set<Double> sortedSet = new TreeSet<Double>(valueList);
Object[] sortedArray = sortedSet.toArray();
int size = sortedArray.length;
for (int i = 0; i < size; i++) {
map.put(keyList.get(valueList.indexOf(sortedArray[size-i-1])), (Double) sortedArray[size-i-1]);
}
return map;
}
A voir également:
- Tri de map sur les valeur
- Google map satellite gratuit - Guide
- Comment faire un tri personnalisé sur excel - Guide
- Logiciel tri photo - Guide
- Telecharger carte google map - Guide
- Google map ma position - Guide
2 réponses
Je ne comprends pas trop quel résultat tu voudrais obtenir.
L'inverse d'une Map<K,V> devrait être quelque chose comme Map<V,Set<K>>
L'inverse d'une Map<K,V> devrait être quelque chose comme Map<V,Set<K>>
public static <K,V> Map<V,Set<K>> inverser(Map<K,V> map) { Map<V,Set<K>> result = new TreeMap<V,Set<K>>(); for (Entry<K, V> e : map.entrySet()) { if (result.containsKey(e.getValue())) result.get(e.getValue()).add(e.getKey()); else { TreeSet<K> set = new TreeSet<K>(); set.add(e.getKey()); result.put(e.getValue(), set); } } return result; } public static <K,V> Map<K,V> inverser2(Map<V,Set<K>> map) { Map<K,V> result = new TreeMap<K,V>(); for (Entry<V, Set<K>> e : map.entrySet()) for (K k : e.getValue()) result.put(k,e.getKey()); return result; } public static void main(String...args) throws FileNotFoundException { Map<String,Double> map1 = new TreeMap<String,Double>(); map1.put("a", 1.0); map1.put("c", 2.0); map1.put("h", 3.0); map1.put("f", 4.0); map1.put("e", 3.0); map1.put("g", 4.0); map1.put("e", 3.0); map1.put("d", 2.0); map1.put("b", 1.0); System.out.println(map1); // {a=1.0, b=1.0, c=2.0, d=2.0, e=3.0, f=4.0, g=4.0, h=3.0} Map<Double,Set<String>> map2 = inverser(map1); System.out.println(map2); // {1.0=[a, b], 2.0=[c, d], 3.0=[e, h], 4.0=[f, g]} Map<String,Double> map3 = inverser2(map2); System.out.println(map3); // {a=1.0, b=1.0, c=2.0, d=2.0, e=3.0, f=4.0, g=4.0, h=3.0} }La confiance n'exclut pas le contrôle
le resultat ke je ve c'est a partire d'une map je ve avoir une map trier selon les valeurs
j'ai utiliser ton code mais ma map n'a pas ete trié
voici ce ke j'ai recu comme resultat :
Map<Double,Set<String>> map2 = inverser(MapAtrier);
System.out.println("\n map2 = \n"+map2);
Map<String,Double> map3 = inverser2(map2);
System.out.println("\n map3 = \n"+map3);
map2 =
{3.58351893845611=[mohammed],
4.394449154672439=[fatima, hamza],
5.0106352940962555=[selwa],
6.697034247666484=[Najma, wafae],
6.956545443151569=[ouma],
6.962243464266207=[Ali, Aziz],
7.590852123688581=[tanae],
7.949797216161852=[aniss],
8.248529124800218=[adam],
8.332789468417959=[ayman],
8.733916174927524=[hamada],
10.343354087361625=[meryem],
14.859772251882303=[btihal, omar]}
map3 =
{Ali=6.962243464266207,
Aziz=6.962243464266207,
adam=8.248529124800218,
aniss=7.949797216161852,
ayman=8.332789468417959,
btihal=14.859772251882303,
fatima=4.394449154672439,
Najma=6.697034247666484,
hamada=8.733916174927524,
hamza=4.394449154672439,
meryem=10.343354087361625,
mohammed=3.58351893845611,
omar=14.859772251882303,
ouma=6.956545443151569,
selwa=5.0106352940962555,
tanae=7.590852123688581,
wafae=6.697034247666484}
le resultat n'est pa trié
voici ce ke j'ai recu comme resultat :
Map<Double,Set<String>> map2 = inverser(MapAtrier);
System.out.println("\n map2 = \n"+map2);
Map<String,Double> map3 = inverser2(map2);
System.out.println("\n map3 = \n"+map3);
map2 =
{3.58351893845611=[mohammed],
4.394449154672439=[fatima, hamza],
5.0106352940962555=[selwa],
6.697034247666484=[Najma, wafae],
6.956545443151569=[ouma],
6.962243464266207=[Ali, Aziz],
7.590852123688581=[tanae],
7.949797216161852=[aniss],
8.248529124800218=[adam],
8.332789468417959=[ayman],
8.733916174927524=[hamada],
10.343354087361625=[meryem],
14.859772251882303=[btihal, omar]}
map3 =
{Ali=6.962243464266207,
Aziz=6.962243464266207,
adam=8.248529124800218,
aniss=7.949797216161852,
ayman=8.332789468417959,
btihal=14.859772251882303,
fatima=4.394449154672439,
Najma=6.697034247666484,
hamada=8.733916174927524,
hamza=4.394449154672439,
meryem=10.343354087361625,
mohammed=3.58351893845611,
omar=14.859772251882303,
ouma=6.956545443151569,
selwa=5.0106352940962555,
tanae=7.590852123688581,
wafae=6.697034247666484}
le resultat n'est pa trié
je suis tt a fait d'acord avec ce ke tu dis, mais le resultat ke j'en ai besoin ne doit pas avois des paire comme dans map2, mais plutot je ve mon resultat soit comme le suivant :
map3 =
{omar=14.859772251882303,
btihal=14.859772251882303,
meryem=10.343354087361625,
hamada=8.733916174927524,
ayman=8.332789468417959,
adam=8.248529124800218,
aniss=7.949797216161852,
tanae=7.590852123688581,
Najma=6.697034247666484,
wafae=6.697034247666484,
Ali=6.962243464266207,
Aziz=6.962243464266207,
ouma=6.956545443151569,
selwa=5.0106352940962555,
fatima=4.394449154672439,
hamza=4.394449154672439,
mohammed=3.58351893845611}
merci bcp pour votre aide
map3 =
{omar=14.859772251882303,
btihal=14.859772251882303,
meryem=10.343354087361625,
hamada=8.733916174927524,
ayman=8.332789468417959,
adam=8.248529124800218,
aniss=7.949797216161852,
tanae=7.590852123688581,
Najma=6.697034247666484,
wafae=6.697034247666484,
Ali=6.962243464266207,
Aziz=6.962243464266207,
ouma=6.956545443151569,
selwa=5.0106352940962555,
fatima=4.394449154672439,
hamza=4.394449154672439,
mohammed=3.58351893845611}
merci bcp pour votre aide
Tu peux essayer comme ça alors :
public static <K extends Comparable<K>,V extends Comparable<V>> Set<Entry<K,V>> sortByValue(Map<K,V> map) { Comparator<Entry<K,V>> comparator = new Comparator<Entry<K,V>>() { @Override public int compare(Entry<K,V> e1, Entry<K,V> e2) { int n = e2.getValue().compareTo(e1.getValue()); if (n==0) return e1.getKey().compareTo(e2.getKey()); else return n; } }; TreeSet<Entry<K,V>> set = new TreeSet<Entry<K,V>>(comparator); set.addAll(map.entrySet()); return set; } System.out.println(sortByValue(map1)); // [f=4.0, g=4.0, e=3.0, h=3.0, c=2.0, d=2.0, a=1.0, b=1.0]