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
| public static void main(String[] args) throws IOException {
Path path_br = Paths.get("files/br.txt");
List<String> lines_br = Files.readAllLines(path_br, StandardCharsets.ISO_8859_1);
Map<String, List<String>> map = new HashMap<>();
for(int i = 0; i<lines_br.size(); i++){
String line = lines_br.get(i);
String str = line; // pourquoi créer autant de chaînes ? tu peux faire directement le traitement sur line...
String[] splitted = line.split("->");
add(map, splitter[1], splitted[0]);
}
}
public static void add(Map<String,List<String>> map, String key, String value) {
List<String> list = map.get(key); // on cherche la liste associée à key
if ( list==null ) { // si la liste est null, c'est qu'il n'y a pas encore de liste associée
list = new ArrayList<>(); // on la créé donc
map.put(key, list); // on la met dans la map, en l'associant à la clef
}
list.add(value); // on ajoute la valeur dans la liste
} |
Partager