Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add elements to an arraylist in java inside a for loop without concurrentmodificationexception

I have a Java Spring MVC Web application. I am trying to iterate through an ArrayList and add new elements to the list based on certain conditions. I use the following code:

List<LocationHourListHB> locationHoursList = new ArrayList<LocationHourListHB>();
List<HoursTO> hourList = listHoursByEntityId(applicationId, siteId, locationId);
for (HoursTO hoursTO : hourList) 
{
    if(locationHoursList.size() == 0)
    {
        LocationHourListHB locationHourListHB = new LocationHourListHB();
        locationHourListHB.setStartTIme(hoursTO.getStartTime());
        locationHourListHB.setEndTime(hoursTO.getEndTime());
        locationHoursList.add(locationHourListHB);
        break;
    }
    for (LocationHourListHB locationHour : locationHoursList) 
    {
        if(hoursTO.getStartTime().equalsIgnoreCase(locationHour.getStartTIme()) && hoursTO.getEndTime().equalsIgnoreCase(locationHour.getEndTime()))
        {
            break;
        }
        else
        {
            LocationHourListHB locationHourListHB = new LocationHourListHB();
            locationHourListHB.setStartTIme(hoursTO.getStartTime());
            locationHourListHB.setEndTime(hoursTO.getEndTime());
            locationHoursList.add(locationHourListHB);
        }
    }
}
}

But executing this throws a concurrent modification exception. I have done some research and found that it can be solved using an iterator or list iterator. Is there a good example to follow for using the list iterator to solve my issue. Or is there any better solution that could save me.

I have tried the following code:

for (HoursTO hoursTO : hourList) 
{
    if(locationHoursList.size() == 0)
    {
        LocationHourListHB locationHourListHB = new LocationHourListHB();
        locationHourListHB.setStartTIme(hoursTO.getStartTime());
        locationHourListHB.setEndTime(hoursTO.getEndTime());
        locationHoursList.add(locationHourListHB);
        }
        ListIterator<LocationHourListHB> iter = locationHoursList.listIterator();
        while(iter.hasNext())
        {
        if(!(iter.next().getStartTIme().equalsIgnoreCase(hoursTO.getStartTime()) && iter.next().getEndTime().equalsIgnoreCase(hoursTO.getEndTime())))
        {
            LocationHourListHB locationHourListHB = new LocationHourListHB();
            locationHourListHB.setStartTIme(hoursTO.getStartTime());
            locationHourListHB.setEndTime(hoursTO.getEndTime());
            iter.add(locationHourListHB);
        }
    }
}

But getting a NoSuchElementException

like image 352
Geo Thomas Avatar asked Dec 22 '25 08:12

Geo Thomas


1 Answers

You can use ListIterator like this:

ArrayList<LocationHourListHB> locationHoursList = new ArrayList<LocationHourListHB>();

ListIterator<LocationHourListHB> iterator = locationHoursList.listIterator();

while(iterator.hasNext(){
LocationHourListHB locationHour = iterator.next()
  if(hoursTO.getStartTime().equalsIgnoreCase(locationHour.getStartTIme()) && hoursTO.getEndTime().equalsIgnoreCase(locationHour.getEndTime()))
        {
            break;
        }
        else
        {
            LocationHourListHB locationHourListHB = new LocationHourListHB();
            locationHourListHB.setStartTIme(hoursTO.getStartTime());
            locationHourListHB.setEndTime(hoursTO.getEndTime());
            iterator.add(locationHourListHB);
        }

}

@Geo Thomas I see you were trying to edit the answer, for this is better to write into comments or update your own question. The problem with the code you send into edit is that you are calling iterator.next() mutltiplle times after iterator.hasNext(). Every time you call iterator.next() it will return the next element in the list, not the same one!

you should use it like this:

LocationHourListHB locationHour = iterator.next()

ANd then use in the code locationHour.

like image 189
HPCS Avatar answered Dec 23 '25 23:12

HPCS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!