MoeBoeMe
MoeBoeMe

Reputation: 133

HashSet equals not working properly

I'm making a test for a little more complex task in where I might have repeated elements that I want to avoid using a HashSet.


I wrote the following code which uses getAllEmpresas(), Empresa or Organization is a more complex structure that a DAOJson is retrieveng, but I'm only interested in the names of those organizations...

   public class TestListaDeNombresDeEmpresas {

    RepositorioDeEmpresas repo ;
    HashSet<String> nombresDeEmpresas ;
    List<Empresa> empresas ; 

    public TestListaDeNombresDeEmpresas () throws IOException
    {
        DAOJsonEmpresa dao = new DAOJsonEmpresa() ;
        repo = RepositorioDeEmpresas.getInstance(dao) ;
        empresas = repo.getAllEmpresas() ;
        nombresDeEmpresas = new HashSet<String>();
        fillHashSet();

    }

    public static void main (String [] args) throws IOException
    {
        TestListaDeNombresDeEmpresas test = new TestListaDeNombresDeEmpresas() ;
        System.out.println("Imprimiendo lista de empresas");
        test.imprimirListEmpresas();
        test.fillHashSet();
        System.out.println();
        test.hashSetContainsFirstElement();
        System.out.println("imprimiendo hash...");
        test.imprimirHashNombresEmpresas();
    }

    public void fillHashSet() {

        List<String> nombresDeEmpresasAux = empresas.stream()
                                .map(e -> e.getName()).collect(Collectors.toList()) ;

        nombresDeEmpresasAux.forEach(e -> nombresDeEmpresas.add(e));

    }

    public void hashSetContainsFirstElement()
    {
        List<String> nombresDeEmpresasAux = empresas.stream()
                .map(e -> e.getName()).collect(Collectors.toList()) ;

        System.out.printf("Contiene el hash de empresas el nombre %s? : %s\n", 
                nombresDeEmpresasAux.get(0),nombresDeEmpresas
                .contains( nombresDeEmpresasAux.indexOf(0) ) );
        System.out.println();
    }

    public void imprimirHashNombresEmpresas()
    {
        nombresDeEmpresas.forEach(System.out::println);
    }

    public void imprimirListEmpresas()
    {
        empresas.forEach(e -> System.out.println( e.getName() ) ) ;
    }
}

The thing is that when I run

public void hashSetContainsFirstElement()
        {
            List<String> nombresDeEmpresasAux = empresas.stream()
                    .map(e -> e.getName()).collect(Collectors.toList()) ;

            System.out.printf("Contiene el hash de empresas el nombre %s? : %s\n", 
                    nombresDeEmpresasAux.get(0),nombresDeEmpresas
                    .contains( nombresDeEmpresasAux.indexOf(0) ) );
        }

it prints false, which doesn't make any sense, actually the output of the program is...

    Imprimiendo lista de empresas
Nike
Facebook
Dolce&Gabbana
Twitter
Snapchat
YouTube

Contiene el hash de empresas el nombre Nike? : false

imprimiendo hash...
Nike
Dolce&Gabbana
Twitter
Facebook
YouTube
Snapchat

Which clearly shows that the HashSet nombresDeEmpresas, actually contains the name 'Nike'... why is this error happening and how can I fix it?

Upvotes: 0

Views: 204

Answers (1)

MiguelKVidal
MiguelKVidal

Reputation: 1538

In your line:

System.out.printf("Contiene el hash de empresas el nombre %s? : %s\n", 
            nombresDeEmpresasAux.get(0),nombresDeEmpresas
            .contains( nombresDeEmpresasAux.indexOf(0) ) );

Do you want to use nombresDeEmpresasAux.get(0) instead of nombresDeEmpresasAux.indexOf(0)?

Upvotes: 1

Related Questions