My question is How can I show only the records I have in my hashmap without repeating them? Ex: I have a arraylist<String>
loaded with data, I also have a text file loaded with data inside.
- File content .txt :
25, Argentina
45, SAN SALVADOR
32, VENEZUELA.
- Content
arraylist
25_ARGENTINA
45_SAN SALVADOR
How will you see if I load a map of my text file and compare the keys (two-digit numeric digits) with what I have in my arraylist
should show me the matching countries to those keys, in this case it would be ARGENTINA and SAN SALVADOR
But instead of that I get:
Country: ARGENTINA
Country: ARGENTINA
Country: ARGENTINA
Country: null
Country: SAN SALVADOR
Country: SAN SALVADOR
I have a method that reads the text file, loads it in a String, then loads the stored in the String in a hashmap, before I go through the arraylist and get the two-digit digits and store it in keyArray :
String keyArray;
public void leer(ArrayList<String> arrayList) {
Map<String, String> mapaCodigosArchivo = new HashMap();
//Recorremos el arrayList
for (String nombreArchivo : arrayList) {
String[] separador = nombreArchivo.split("_");
String codPaises = separador[0];
keyArray = codPaises;
}
try {
// Abrimos el archivo con la ruta especificada.
FileInputStream fstream = new FileInputStream(new File("Paises.txt"));
// Creamos el objeto de entrada
DataInputStream entrada = new DataInputStream(fstream);
// Creamos el Buffer de Lectura
BufferedReader buffer = new BufferedReader(new InputStreamReader(entrada));
String contenido;
// Leer el archivo linea por linea
while ((contenido = buffer.readLine()) != null) {
String[] separador = contenido.split(",");
mapaCodigosArchivo.put(separador[0], separador[1]);
//Imprimimos los resultados que coinciden con la clave de dos digitos.
System.out.println("Sucursal: " + mapaCodigosArchivo.get(keyArray));
}
// Cerramos el archivo
entrada.close();
} catch (Exception e) { //Catch de excepciones
System.err.println("Ocurrio un error: " + e.getMessage());
}
}
And researched but I do not give with the correct answer, I hope you can help me. Thanks again.