I am making a foreach
in hashmap
based on the following arquivo.txt
:
chaveA;6
chaveA;4
chaveB;3
chaveB;7
chaveC;1
chaveC;1
chaveD;5
For the time being my code looks 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 the Map. With this in mind, two problems arise.
First: The map returns me 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?