How can I make sure the items in the list are consistent?
4 answers
You can override ArrayList, set and add methods to check if the set / added element is valid, and throw an IllegalArgumentException if it is not.
Why not have a collection that is always sorted.
SortedSet<Domain> items = new TreeSet<Domain>();
This way, your added element will always be in the right place.
0
source to share
If you want to get an exception when replacing an element as you described, the best way is to expand or wrap the list and override the add methods.
If all you really need is a sorted list, use one of the SortedSets .
0
source to share
If you are using Java 7 try using:
http://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html
0
source to share