3
I have a txt file with a line-separated user name below:
diego
sergio
antonio
maria
diego
antonio
Notice that names can repeat, and I would like to count and list only the distinct names.
I did this method to list the entire file:
String strPath = DIRETORIO + ARQUIVO_FILE;
if (pathExists(strPath)) {
List<String> texto = Files.readAllLines(new File(strPath).toPath());
for (String linha : texto) {
System.out.println(linha);
}
} else {
System.out.println("arquivo não existe");
}
but I’m not sure how to adapt it into another method that makes this count of different names. How do I count this?
Note: some names may come with a point separating type surname
diego.felipe
, but each name and/or surname is saved per line only.
You want, at the end of it all, to have the total number of names without counting the repeated ones, right?
– Jéf Bueno
The first thing that came to mind was to use key-value lists, Hashmap in Java for example.
– Pedro Teles
@jbueno exactly that
– user28595
@Diegofelipe Ok, I made an answer with this. See if it helps you.
– Jéf Bueno