I am trying to create a list that holds up to the latest 10 values, however, these values need to be the latest. For example, I have a loop that outputs 100 numbers but only the lastest 10 numbers should be saved in this list.
Can anyone help me with this scenario as I am stuck and new to JAVA?
2
Answers
Try Guava library it has
EvictingQueue
It’s Android compatible, but
'com.google.common.collect.EvictingQueue' is marked unstable with @Beta
:You can simply create a List with limitation, by extending an existing List-Implementation and overriding the
add
-method like this:Note: in
ArrayList
there are also other methods to add elements, so maybe these also need to be overriden.