Issue
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?
Solution
Try Guava library it has EvictingQueue
EvictingQueue<String> queue = EvictingQueue.create(10);
//code test
//add 100 strings to queue
for(int i =0;i<100;i++){
queue.add ("some text " + i);
}
//Iterate through array and print
for(String x: queue){
System.out.println (x);
}
Output:
Some Text90
Some Text91
Some Text92
Some Text93
Some Text94
Some Text95
Some Text96
Some Text97
Some Text98
Some Text99
It's Android compatible, but 'com.google.common.collect.EvictingQueue' is marked unstable with @Beta
:
implementation("com.google.guava:guava:30.1.1-android")
Answered By - GremlinShX
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.