tags:

views:

46

answers:

1

I know how to limit size in Map (like this,using LinkedHashMap.removeEldestEntry method does exactly that)

I want to know how to limit size in a List,what is a best way to implement?

thanks for help :)

A: 

I would look at the java.util.Collections class source and develop a SizeLimitedList similar to how they do a checkedList. Then on add I would delete the first entry from the list if the list was full.

TofuBeer
`if(list.size()>10){list.remove(0);}` like this?
Zenofo
you would do that in the overridden add method. you would need to look at addAll too.
TofuBeer
You could just derive from ArrayList, and override add and addAll and do the above.
MeBigFatGuy
It is more flexible to do the other way though...
TofuBeer