How to keep the last 100 objects inserted in an Arraylist?

Asked

Viewed 116 times

-4

How to keep the last 100 objects inserted in an Arraylist? This will be constantly fed but I want to keep only the last 100.

  • 1

    What is the doubt?

  • 1

    An Arraylist always adds new elements to the end of the queue. You will have to create a method that treats the addition, checking first if the list already has 100 items and then removing the first one before adding a new one, using something like if(lista.size == 99){lista.remove(0);}lista.add(element);

  • 1

    I think it is extremely important that you solve this problem on your own. Then, throw in the code that you got, which agent helps.

2 answers

1

The simplest way to solve this problem is to apply the concept of FIFO (First in, first out).

Fifo refers to a row-type data structure. Lists are widely used in programming to implement queues. In a FIFO type queue the elements are placed in the queue and removed (or processed) in order of arrival. The basic idea of the queue is that we can only insert a new element at the end of the queue and we can only remove the element from the beginning.

Exemplo de execução de um código FIFO

You can see more here

In addition to this concept it will be necessary to limit the volume of records you want to store, in this way use the Apache Commons Collections

If you are using Maven you can import:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-collections4</artifactId>
    <version>4.1</version>
</dependency>

If you are using Gradle

`compile group: 'org.apache.commons', name: 'commons-collections4', version: '4.1'`

Basically you just need to add the library commons-collections4 in their dependencies, for example I used summer 4.1.

After doing import you can create your previously limited lists for example:

 new CircularFifoQueue<String>(100)
 new CircularFifoQueue<Integer(100)

among others.

To illustrate better I made an example by creating random string and storing in the queue, at the end I display the remaining values:

public static void main(String args[]) {
    Collection<String> fifo = new CircularFifoQueue<String>(100);
    for (int i = 1; i <= 500; i++) {
        String id = UUID.randomUUID().toString();
        System.out.println(i + " - " + id);
        fifo.add(id);
    }

    System.out.println("----> Result Fifo <----");
    int i = 1;
    for (String id : fifo) {
        System.out.println(i++ + " - " + id);
    }
}
  • Excellent and more elegant! .. I will wear it!

-1

I got what I wanted with while..

public static void main(String[] args) {
    // TODO Auto-generated method stub

    ArrayList<String> arr = new ArrayList<>();

    for(int i = 0;i < 150 ; i++){
        arr.add(Integer.toString(i));
    }

    while(arr.size() >= 100){
        arr.remove(0);
    }

    for(String x : arr){
        System.out.println(x);
    }

}
  • 3

    Just one fix: this site IS NOT A FORUM.

  • 2

    Simply put in the answer only what answers the question. Use the comments (when you have a reputation for it) to make additional comments, which are not up to the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.