java - JavaFX ConcurrentModificationException withouth (knowingly) implemtented threads -


first of objects:

public class group {      private final observablelist<idevice> sourcelist;      private final observablelist<idevice> destinationlist;      private final observablelist<mapping> mappinglist; ...}  public class mapping {      private final idevice source;      private final idevice destination;      private final mappingmode mode;      public final stringproperty sourcename = new simplestringproperty();      public final stringproperty destinationname = new simplestringproperty();      public final stringproperty modename = new simplestringproperty(); ...} 

basically group contains 2 lists of idevices can either source or destination , mapping list contains 1 of them , 1 of 2 modes (enum).

the idevice lists displayed in own listview table between them, representing mapping (containing 1 column first, 1 second list , mode column).

i have added them via setitems, cellfactory listviews

private callback<listview<idevice>, listcell<idevice>> getfullnamedisplay() {     return new callback<listview<idevice>, listcell<idevice>>() {         @override         public listcell<idevice> call(listview<idevice> p) {             listcell<idevice> cell = new listcell<idevice>() {                 @override                 protected void updateitem(idevice t, boolean bln) {                     super.updateitem(t, bln);                     if (t != null) {                         settext(t.getfullname());                     }                     else                         settext("");                 }             };             return cell;         }     }; } 

the columns set this:

sourcecolumn.setcellvaluefactory(celldata -> celldata.getvalue().sourcename); destinationcolumn.setcellvaluefactory(celldata -> celldata.getvalue().destinationname); modecolumn.setcellvaluefactory(celldata -> celldata.getvalue().modename); 

i added 2 buttons each listview add , remove new items.

of course if remove source or destination device, want of mappings removed, added listchangelistener 2 lists:

 private listchangelistener<idevice> getdevicechangelistener() {     return (javafx.collections.listchangelistener.change<? extends idevice> c) -> {         while (c.next()) {             if (c.wasremoved()) {                 c.getremoved().stream().foreach((d) -> {                     mappinglist.stream().filter((map) -> (map.getsource().equals(d) || map.getdestination().equals(d))).foreach((map) -> {                         mappinglist.remove(map);                     });                 });             }         }     }; } 

this intended (and refactorings tried did), cant why invokes (most of time) concurrentmodificationexception have not yet used threading in application. seems doesnt trigger each time, understand can lucky scheduling if using threads.. result correct though

someone clue?

thanks in advance

you cannot modify collection while iterating through it, unless modification done via iterator. in java 8, collection class introduced removeif(...) method helps in use case:

private listchangelistener<idevice> getdevicechangelistener() {     return (javafx.collections.listchangelistener.change<? extends idevice> c) -> {         while (c.next()) {             if (c.wasremoved()) {                 c.getremoved().foreach(d ->                      mappinglist.removeif(map -> map.getdestination().equals(d)                                               || map.getsource().equals(d)));             }         }     }; } 

Comments