For don’t go through the whole list

Asked

Viewed 60 times

1

I’m having a problem going through a list.

Man for this way:

 for (int i = 0; i < listaCaixaAbertos().size(); i++) {
                listaCaixaAbertos().get(i).setFechado(Boolean.TRUE);
                salvar(listaCaixaAbertos().get(i));
            }

My list goes like this:

 public List<LancamentoCaixa> listaCaixaAbertos() {
    Query q = em.createQuery("FROM LancamentoCaixa As a WHERE a.fechado = false");
    return q.getResultList();
}

Every time I go through the list I want to set false to the variable fechado.

However to it Does not change all the data of this list, to change all I have to click several times depending on the amount of items.

1 answer

3


I think this is what you want:

for (LancamentoCaixa lancamento : listaCaixaAbertos()) {
    lancamento.setFechado(true);
    salvar(lancamento);
}

I put in the Github for future reference.

Whenever possible it is best to for that runs through the entire collection. This alone eliminates the problems. I was having them list everything to get the size, then list it again to get the item and change it, and then list it again to save. This is not only semantically wrong, it can create unwanted side effects, running condition and the performance will suffer.

  • Just what I needed! Problem solved, thank you very much.

Browser other questions tagged

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