Jan 30

The general cause of a ConcurrentModificationException is from trying to remove an instance from a collection that is being iterated over as in the following example.


Iterator i = mylist.iterator();
while (i.hasNext())
{
   Object o = i.next();
   if (something)
   {
       // following line will throw ConcurrentModificationException
       mylist.remove(o);
   }
}

The correct way is to instead use the Iterator’s remove() method as shown in the following snippet:


Iterator i = mylist.iterator();
while (i.hasNext())
{
   Object o = i.next();
   if (something)
   {
       i.remove();
   }
}

written by objects \\ tags: , ,

Oct 01

The following custom iterator allows iteration through a date range.


import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;

public class DateIterator
   implements Iterator<Date>, Iterable<Date>
{

    private Calendar end = Calendar.getInstance();
    private Calendar current = Calendar.getInstance();

    public DateIterator(Date start, Date end)
    {
        this.end.setTime(end);
        this.end.add(Calendar.DATE, -1);
        this.current.setTime(start);
        this.current.add(Calendar.DATE, -1);
    }

    public boolean hasNext()
    {
        return !current.after(end);
    }

    public Date next()
    {
        current.add(Calendar.DATE, 1);
        return current.getTime();
    }

    public void remove()
    {
        throw new UnsupportedOperationException(
           "Cannot remove");
    }

    public Iterator<Date> iterator()
    {
        return this;
    }

    public static void main(String[] args)
    {
    	Date d1 = new Date();
    	Calendar cal = Calendar.getInstance();
    	cal.add(Calendar.DATE, 20);
    	Date d2 = cal.getTime();

    	Iterator<Date> i = new DateIterator(d1, d2);
    	while(i.hasNext())
    	{
    		Date date = i.next();
    		System.out.println(date);
    	}
    }
} 

written by objects \\ tags: , ,