Count distinct names saved in txt

4

I have a txt file with users' names separated by line as follows:

diego
sergio
antonio
maria
diego
antonio

Note that names can repeat, and I would like to list and list only the distinguished names.

I made this method to list the whole 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 to another method that does this distinct name count. How do I make this count?

  

Note: some names may come with a dot separating surname type    diego.felipe , but each name and / or firstname is saved per line   only.

    
asked by anonymous 16.02.2016 / 11:19

4 answers

3

Create a list of strings to save names, validate if the name no longer exists within the list before adding it, if it does not exist, add the name to the list. At the end of the iteration, use lista.size() to get the sum.

List<String> nomes = new ArrayList<String>();

String strPath = DIRETORIO + ARQUIVO_FILE;
if (pathExists(strPath)) 
{
    List<String> texto = Files.readAllLines(new File(strPath).toPath());
    for (String linha : texto) 
    {            
        if(!nomes.contains(linha)){
            nomes.add(linha);
        }
    }

    System.out.println("Total de nomes: " + nomes.size());    

}
else 
{
    System.out.println("arquivo não existe");
}
    
16.02.2016 / 11:28
7

You can use the HashSet collection, where elements are kept uniquely. If you try to insert an existing element, it is not added.

The interesting thing about this collection is that the basic operations add , remove , contains and size have asymptotic complexity of O(1) .

HashSet<String> nomes = new HashSet<String>();

String strPath = DIRETORIO + ARQUIVO_FILE;
if (pathExists(strPath)) 
{
    List<String> texto = Files.readAllLines(new File(strPath).toPath());
    for (String linha : texto) 
    {   
        nomes.add(linha);
    }

    System.out.println("Total de nomes: " + nomes.size());    

}
else 
{
    System.out.println("arquivo não existe");
}
    
16.02.2016 / 15:56
3

Look, I do not know how it works in Java. But in C # there is a set called HashSet that does not save repeated elements. In a way you will need to do something like this for Java.

HashSet<string> devedores = new HashSet<string>();
// Podemos adicionar elementos no conjunto utilizando o método Add
devedores.Add("victor");
devedores.Add("osni");

// Para sabermos o número de elementos adicionados, utilizamos a propriedade
// Count do conjunto. Nesse exemplo elementos guardará o valor 2
int elementos = devedores.Count;

// O conjunto não guarda elementos repetidos, então se tentarmos
// adicionar novamente a string "victor", o número de elementos
// continua sendo 2
devedores.Add("victor");
    
16.02.2016 / 11:42
0

You can further simplify the solution with HashSet by passing the List of reading the file direct to the HashSet constructor:

String strPath = DIRETORIO + ARQUIVO_FILE;
if (pathExists(strPath)) {
    Set<String> nomes = new HashSet<>(Files.readAllLines(new File(strPath).toPath()));
    System.out.println("Total de nomes: " + nomes.size());    
} else {
    System.out.println("arquivo não existe");
}
    
06.08.2018 / 14:00