Concurrentmodificationexception how to proceed?

Asked

Viewed 1,134 times

1

I have the following method:

private void houveAlteracao(Long id) {
        try {
            Objeto objeto = service.find(id);
            objeto.setEventos(JsfUtils.ordenarListaEventosPorHora(objeto.getEventos()));
            Objeto obj = popularObjeto(objeto.getCodigo());
            obj.setEventos(JsfUtils.ordenarListaEventosPorHora(obj.getEventos()));
            if (obj.getEventos().size() > objeto.getEventos().size()) {
                for (Evento ev : obj.getEventos()) {
                    for (Evento e : objeto.getEventos()) {
                        if (!ev.getHorario().equals(e.getHorario())) {
                            objeto.getEventos().add(ev);
                            ev.setObjeto(objeto);
                            if (ev.getDestino() != null) {
                                ev.getDestino().setEvento(ev);
                            }
                        }
                    }
                }
                service.update(objeto);
                pesquisar();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

The error is occurring:

13:18:23,335 ERROR [stderr] (default task-2) java.util.Concurrentmodificationexception

13:18:23,336 ERROR [stderr] (default task-2) at java.util.Arraylist$Itr.checkForComodification(Arraylist.java:901)

13:18:23,337 ERROR [stderr] (default task-2) at java.util.Arraylist$Itr.next(Arraylist.java:851)

I read that the error occurs because of changing a list in iteration, someone knows how to solve ?

2 answers

0

I appreciate the comments but based on some I thought about this logic, I accept criticism if I made a mistake but now it is working once I order the lists to match the values correctly. I did in this structure since both lists have equal values so I only complement 2 with the missing:

private void houveAlteracao(Long id) {
        try {
            Objeto objeto = service.find(id);
            objeto.setEventos(JsfUtils.ordenarListaEventosPorHora(objeto.getEventos()));
            Objeto obj = popularObjeto(objeto.getCodigo());
            obj.setEventos(JsfUtils.ordenarListaEventosPorHora(obj.getEventos()));
            if (obj.getEventos().size() > objeto.getEventos().size()) {
                int contador = obj.getEventos().size() - objeto.getEventos().size();
                int inicioIndice = obj.getEventos().size() - contador;
                for (int i = inicioIndice; i < obj.getEventos().size(); i++) {
                    Evento evento = obj.getEventos().get(i);
                    evento.setObjeto(objeto);
                    if (evento.getDestino() != null) {
                        evento.getDestino().setEvento(evento);
                    }
                    objeto.getEventos().add(evento);
                }
                service.update(objeto);
                pesquisar();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

0

You can use the interface Iterator<T> to iterate the list you want to modify.

Instead of doing for (Evento e : objeto.getEventos()) { }, do:

Iterator<Evento> iterator = objeto.getEventos().iterator();
while(iterator.hasNext()) {
    Evento e = iterator.next(); //se quiser usar a iteração atual
    iterator.add(ev);
}

This adds the object ev at the iterator during the iteration.


If you want the item to be actually added to the list (to use after it exits the repeat loops, for example), think that you need to add these items to a "temporary list" during iterations and then make a addAll() in the original list. I’m not sure if the Iterator does the bind back to the list when it is modified.

  • Thanks for the answer, I’ll test it later. A doubt, I can’t do anything on the list during iteration(change,add,remove) as I was doing ?

  • Quiet! ;). No. you cannot touch the list, even using the Iterator. It is based on its original list. If it moves will release the same Exception

  • If you can’t touch the list because you’re going to release the same Exception, how does your answer solve the question?

  • @ramaral he wants to add an item in the list that is being iterated during the iteration. The way I posted it is possible to reproduce this behavior with the Iterator. The question is not clear whether he actually wants to make it work by going through the list or whether he wants to make it work in other ways. If he wants to touch the list, in fact, the answer is not satisfactory. If not, the answer applies

  • What you propose is not much different than what he has. If the mistake is because of tampering with the list it is best to use one for traditional instead of foreach

  • Got it. With everything, this solution worked for me in similar situations. If you believe you have a better solution, feel free to post.

Show 1 more comment

Browser other questions tagged

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