ConcurrentModificationException how to proceed?

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.checkForComification (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, does anyone know how I can do it?

    
asked by anonymous 03.04.2017 / 18:21

2 answers

0

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

Instead of 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 to iterator during iteration.

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

    
03.04.2017 / 18:41
0

Thanks to the comments but based on some even I thought of this logic, I accept criticism if I made some mistake but now it is working once I order the lists so that the values strike right. I made this structure since both lists have equal values, so I only complement the 2 with the missing ones:

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();
        }

    }
    
04.04.2017 / 01:17