I want a Collection that stores the 10 most recent history items of something.
When a new item is added, I want to add the item to the beginning of the list and, if the list size is greater than 10, I need to trim the list down.
Further if an item is added to the top of the list that already exists, I want to remove the other instance (or I could simply check during insertion if the item already exists and instead change the order to move it to the top of the list).
I started looking through the fun new types of Queues now available in Java 5, but I think that's the wrong direction.
There's a good chance I'll end up extending ArrayList but I thought maybe I could get lucky and grab advice from a Collections guru.
Don't extends ArrayList (or LinkedList, or Vector). If you do, that does not stop people from inserting items in the middle of your list, unless you override the indexed add, remove and set methods as well.
You could of course use an ArrayList internally, and delegate all of the hard work to it.
I would suggest you to have an array in your special collection class internally and play with that array as per your requirements since here you are aware of this fact that size is going to be constant so I think array will be much better than any other collection in terms of performance.
I agree with Jammy, if the maximum number of elements is fixed, I'd take an array. But when it should be a collection, I would extend a LinkedList rather than a LinkedHashSet. Set does care about the uniqueness of the elements of its own - however it's add method cannot not care about the repositioning of your doublet history element. THe bounded fifo buffer is also not applicable directly, so:
I'm not really sure how to handle the super class's method addFirst(). Should it be allowed?
In the constructors of the class you cannot hand over the collections from the arguments as super(c) because the addAll method is overridden and in the constructor of the super class it would be called polymorphically with a maximum value of 0. This is also the cause why the addAll is needed in the constructor.