alex alex - 1 year ago 92
Java Question

Create a java List with maximum of 100 values

I create a list of object with java

public class Cleint {

private int id;
private String user;
private int age;

public static List<Client> last100CleintList;

The list is supposed to be a kind of queue where the last 100 clients are stored. So when the list is empty I just want to add clients but when I reach 100 I want to delete the last one and add the new one. I could do it manually but is there a function for that? Or maybe in arrays, I am not forced to use lists.

Answer Source

There is no built-in library to achieve that (data-structure is there) without creating a utility method yourself.

Since you want to keep the last 100Clients every-time you append; and the list size is 100, you have to remove the first Client. You could try something like this (with Client objects).

    import java.util.Queue;
    import org.apache.commons.collections4.queue.CircularFifoQueue;`

    Queue<String> circularQueue = new CircularFifoQueue<String>(2);



outputs ["Doe", "Joe"];

You can also do this with:

  2. MinMaxPriorityQueue by guava
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download