Compare the data of an Arraylist not to let register in duplicity - Java

Asked

Viewed 42 times

0

Good afternoon to all

I need to compare all the indexes of this arrayList with the Indice that is currently being added, it is possible?

//MENU - CADASTRAR PRODUTOS
       private void cadastrarProdutos() {
            String escolha;
            do {
                tituloPrincipal();

                System.out.println("**** INCLUSÃO DE PRODUTOS ****");
                Mercadorias mercadoria = setDadosDoProduto();
                escolha = confirmaOperacao();
                if (escolha.equalsIgnoreCase("S")) {
                    for(int i = 0; i < prodList.size(); i++) {            
                            if(prodList.get(0).getNome() == prodList.get(i).getNome()) {
                                System.out.println("Esse produto já foi cadastrado!");
                            }
                            else {
                                prodList.add(mercadoria);
                            }
                        }                         
                }
                escolha = getRepetirOperacao();

            } while (escolha.equalsIgnoreCase("S"));
            adicionarProduto();
        }

inserir a descrição da imagem aqui

2 answers

4

If you do not want your list to contain duplicate elements, the correct collection to use is Set. How your code does not contemplate the creation of the variable prodList, I will make the following general suggestion for changing the code:

Set<Mercadorias> prodList = new HashSet<>();

In order for the collection to correctly verify that the item already exists in its elements, you must override the method hashCode and equals of your class Mercadorias. Add the following code to it:

@Override
public int hashCode() {
  return this.getNome().hashCode();
}

@Override
boolean equals(Object obj) {
  if (obj == null) {
    return false;
  }

  if (!(obj instanceof Mercadoria)) {
    return false;
  }

  if (obj == this) {
    return true;
  }

  return this.getNome().equals(((Mercadoria) obj).getNome());
}

So after these changes you can adjust your code to:

// ...
if (escolha.equalsIgnoreCase("S")) {
  if (!prodList.add(mercadoria)) {
    System.out.println("Esse produto já foi cadastrado!");
  }
}
// ...

See working on ideone.


Set

A Collection that contains no Uplicate Elements.

In free translation:

A collection that does not contain duplicate elements.

  • 1

    If you’re overwriting Object.hashCode(), need to overwrite too Object.equals().

  • 1

    Done, thank you.

-1

In the stretch

if(prodList.get(0).getNome() == prodList.get(i).getNome()) {

You are always comparing the first item of prodList with each item of the same prodList.

I imagine you’d have to compare it to the name of the new merchandise, for example:

if(mercadoria.getNome() == prodList.get(i).getNome()) {

Another observation:

When comparing objects, we need to use the equals() method in place of ==

if(mercadoria.getNome().equals(prodList.get(i).getNome()))

https://morettic.com.br/wp2/poo/equals-em-java/

Edit: as @Sorack well commented, the function that adds the goods should be used outside the loop for, the way it put, the goods will be added several times, whenever the name of the goods evaluated is different.

  • 1

    Your solution partially corrects the code, but you don’t think it’s going through the list, so the portion of code that does the insertion is in the wrong place.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.