3

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

1
  • 1
    Do not use multiple times iter.next() , see my updated answer. Commented Dec 8, 2018 at 13:50

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

that edit was done by mistake. Thanks for the upadte
1

You can't use iterator, you'll get the same exception if you try to add to the list while iterating, and there's no add method on the iterator.

ListIterator has an add method which adds the new item before the next one.

    List<String> list = new ArrayList<>();
    list.add("one");
    list.add("two");
    ListIterator<String> iterator = list.listIterator();
    while (iterator.hasNext()) {
        String value = iterator.next();
        if (value.equals("one")) {
            iterator.add("three");
        }
    }
    System.out.println(list);  // prints [one, three, two]

If you need to add to the end of the list, use a second list and add them after the loop

    List<String> list = new ArrayList<>();
    List<String> newValues = new ArrayList<>();
    list.add("one");
    list.add("two");
    for (String value : list) {
        if (value.equals("one")) {
            newValues.add("three");
        }
    }
    list.addAll(newValues);
    System.out.println(list);  // prints [one, two, three]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.