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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
public class Arrangement {
public static void main(String[] args) {
final File inFich = new File("liste.txt");
Map<String, LinkedList<String>> multiResMap = new LinkedHashMap<>();
listeFichier(inFich); // juste pour l'affichage des données
multiResMap = transFichier(inFich);
for (String cle : multiResMap.keySet()){
for (String l : multiResMap.get(cle)){
System.out.printf("ResMap \t cle : %s \t valeur associée : %s\n",cle,l);
}
}
}
private static Map<String,LinkedList<String>> transFichier(final File inFich){
Map<String, Integer> cleNumeroLigne = new LinkedHashMap<>(); //stocke la clé et son numéro de ligne
try(InputStream is = new FileInputStream(inFich);InputStreamReader ipsr = new InputStreamReader(is);BufferedReader br = new BufferedReader(ipsr)){
String ligne;
String[] tabCles;
int nbLigne = 0;
while ( (ligne = br.readLine()) != null ){
++nbLigne;
tabCles = ligne.split(",");
for (int i = 0, n = tabCles.length; i< n; ++i){
if (!cleNumeroLigne.containsKey(tabCles[i])){ //Comment traiter les doublons ? (pas d'infos)
cleNumeroLigne.put(tabCles[i],nbLigne);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return transMultiMap(cleNumeroLigne);
}
private static Map<String,LinkedList<String>> transMultiMap(final Map<String,Integer> map){
Map<String,LinkedList<String>> mapRes = new LinkedHashMap<>();
LinkedList<String> tempo;
for (Entry<String, Integer> cle : map.entrySet()){
tempo = new LinkedList<>();
for (Entry<String, Integer> clebis : map.entrySet()){
if (!mapRes.containsKey(clebis.getKey()) & (cle.getValue()) != (clebis.getValue())){
tempo.add(clebis.getKey());
}
}
mapRes.put(cle.getKey(), tempo);
}
return mapRes;
}
private static void listeFichier(final File inFich){
try(InputStream is = new FileInputStream(inFich);InputStreamReader ipsr = new InputStreamReader(is);BufferedReader br = new BufferedReader(ipsr)){
String ligne;
while ( (ligne = br.readLine()) != null ){
System.out.printf("%s\n",ligne);
}
} catch (IOException e) {
e.printStackTrace();
}
}
} |
Partager