Campbell Ritchie wrote:and throw anUnsupportedOperationException from the add(int, E) method. Note your wrapper class will have to implement the List<E> interface too.
Campbell Ritchie wrote:A Comparator consistent with equals does sound a good idea, but if you use it with a TreeSet you won't get any duplicates.
Paul Clapham wrote:There's nothing to stop you from using both compareTo and equals in your Collection, just like the Set and Map implementations use both hashCode and equals.
So you could have an ArrayList in your collection; when something calls your Collection's "add" method you do a binary search to find out where it's going to be added to the ArrayList (using the compareTo method). Then before you add it you use the equals method to see if you really want to add it, or whether it's equal to the entries at (or near?) where you would add it.