1
I’m making a foreach
in a hashmap
on the basis of the following arquivo.txt
:
chaveA;6
chaveA;4
chaveB;3
chaveB;7
chaveC;1
chaveC;1
chaveD;5
For now my code is like this:
Scanner scanArquivo = new Scanner(new File("arquivo.txt"));
HashMap<String, String> mapa = new HashMap<String, String>();
String linha[];
while (scanArquivo.hasNext()) {
linha = scanArquivo.nextLine().split(";");
mapa.put(linha[0], linha[1]);
mapa.forEach((chave, valor) -> {
if (chave.equals("prdA")) System.out.println("Primeira chave");
});
}
scanArquivo.close();
For each line, I split the file to separate the information and play it in Map. Keeping this in mind, two problems arise.
First: The map returns duplicate information. The output of this code shows me more lines than it has in the file itself.
Primeira chave
Primeira chave
Primeira chave
Primeira chave
Primeira chave
Primeira chave
Primeira chave
Second: Here is another algorithm problem. For each different key I want the highest value. How can I implement this?
It helped a lot brother! Vlw really!!
– Gabriel Hardoim