Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

A special Collection class

 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marc

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.
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know how you feel about using the Commons Collections framework.
It has the BoundedFifoBuffer. Even when you won't use it, you might pick up an idea.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy,

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?

Test code:




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.




Yours,
Bu.
 
    Bookmark Topic Watch Topic
  • New Topic