A
Clarification:You say you want a https://es.wikipedia.org/wiki/Last_in,_first_out (last in first out - The last one in is the first one out.), but the example you provide is a https://es.wikipedia.org/wiki/First_in,_first_out (first in first out - the first one in is the first one out) since you are eliminating the A that was the first to enter.But don't worry, whatever your case (thinking that FIFO) someone already took care of the topic and Java has classes for it and, even if they're not exactly what you need, they're going to save us most of the work.FIFO: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Queue.html The Queue object is a particular implementation of LinkedList, can make the stack of objects have its own order based on the equals and hashCode of the objects it contains, but by default, is a FIFO stack as you can see:EJEMPLO:import java.util.LinkedList;
import java.util.Queue;
private final static String[] letras = { "A", "B", "C", "D", "E" };
Queue<String> fifo = new LinkedList<String>();
for (String s : letras) {
fifo.add(s);
}
System.out.println(fifo);
while (!fifo.isEmpty()) {
System.out.println("La que se añadio mas recientemente es " + fifo.remove());
System.out.println(fifo);
}
HEALTH: [A, B, C, D, E]
La que se añadio mas recientemente es A
[B, C, D, E]
La que se añadio mas recientemente es B
[C, D, E]
La que se añadio mas recientemente es C
[D, E]
La que se añadio mas recientemente es D
[E]
La que se añadio mas recientemente es E
[]
LimitedFIFOList<E>From there, with making a wrapper class that protects the add methods, you can control the maximum size and have a limiting getter apart from size(): import java.util.LinkedList;
public class LimitedFIFOList<E> extends LinkedList<E> {
private static final long serialVersionUID = 1L;
private int limite;
public LimitedFIFOList(int limite) {
this.limite = limite;
}
@Override
public boolean add(E o) {
super.add(o);
// si el tamaño fuera mayor eliminamos uno
while (size() > limite) { super.remove(); }
return true;
}
public int getLimite() {
return limite;
}
public void setLimite(int limite) {
this.limite = limite;
}
}
Using the example above:private static void FIFO() {
LimitedFIFOList<TextDataStatus> fifo = new LimitedFIFOList<TextDataStatus>(3);
for (TextDataStatus d : datos) {
fifo.add(d);
System.out.println(fifo.toString());
}
}
HEALTH: [TextDataStatus{action=1, content='A', start=1, end=6}
]
[TextDataStatus{action=1, content='A', start=1, end=6}
, TextDataStatus{action=1, content='B', start=2, end=7}
]
[TextDataStatus{action=1, content='A', start=1, end=6}
, TextDataStatus{action=1, content='B', start=2, end=7}
, TextDataStatus{action=1, content='C', start=3, end=8}
]
[TextDataStatus{action=1, content='B', start=2, end=7}
, TextDataStatus{action=1, content='C', start=3, end=8}
, TextDataStatus{action=1, content='D', start=4, end=9}
]
[TextDataStatus{action=1, content='C', start=3, end=8}
, TextDataStatus{action=1, content='D', start=4, end=9}
, TextDataStatus{action=1, content='E', start=5, end=10}
]
LIFO Collection: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Stack.html Since we are and that the question is a bit confusing, we will also explain the LIFO classes that Java has and how to put a limit to having the pairs complete. To do this, we will use http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Stack.html .The Stack class represents a last-in-first-out (LIFO) stack of objects. It extends class Vector with five operations that allow a vector to be treated as a stack. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack, a method to test for whether the stack is empty, and a method to search the stack for an item and discover how far it is from the top.EJEMPLO:import java.util.Stack;
private final static String[] letras = { "A", "B", "C", "D", "E" };
Stack<String> st = new Stack<String>();
for (String s : letras) {
st.add(s);
}
System.out.println(st.toString()); // [A, B, C, D, E]
// cambiando el tamaño a menos de los objetos que contiene
// elimina los ultimos objetos añadidos
st.setSize(3);
System.out.println(st.toString()); // [A, B, C]
// pop quita el que se haya añadido el ultimo
st.pop();
System.out.println(st.toString()); // [A, B]
HEALTH:[A, B, C, D, E]
[A, B, C]
[A, B]
LimitedLIFOList<E>If your case was this, the wrapper would be very similar, but with Stackimport java.util.Stack;
public class LimitedLIFOList<E> extends Stack<E> {
private static final long serialVersionUID = 1L;
private int limite;
public LimitedLIFOList(int limite) {
this.limite = limite;
}
@Override
public boolean add(E o) {
super.add(o);
// si el tamaño fuera mayor eliminamos uno
while (size() > limite) { super.pop(); }
return true;
}
public int getLimite() {
return limite;
}
public void setLimite(int limite) {
this.limite = limite;
}
}
EJEMPLO:private static void LIFO() {
LimitedLIFOList<TextDataStatus> lifo = new LimitedLIFOList<TextDataStatus>(3);
for (TextDataStatus d : datos) {
lifo.add(d);
System.out.println(lifo);
if (lifo.size() == lifo.getLimite())
System.out.println("quitamos > " + lifo.pop());
}
}
HEALTH:[TextDataStatus{action=1, content='A', start=1, end=6}
]
[TextDataStatus{action=1, content='A', start=1, end=6}
, TextDataStatus{action=1, content='B', start=2, end=7}
]
[TextDataStatus{action=1, content='A', start=1, end=6}
, TextDataStatus{action=1, content='B', start=2, end=7}
, TextDataStatus{action=1, content='C', start=3, end=8}
]
quitamos > TextDataStatus{action=1, content='C', start=3, end=8}
[TextDataStatus{action=1, content='A', start=1, end=6}
, TextDataStatus{action=1, content='B', start=2, end=7}
, TextDataStatus{action=1, content='D', start=4, end=9}
]
quitamos > TextDataStatus{action=1, content='D', start=4, end=9}
[TextDataStatus{action=1, content='A', start=1, end=6}
, TextDataStatus{action=1, content='B', start=2, end=7}
, TextDataStatus{action=1, content='E', start=5, end=10}
]
quitamos > TextDataStatus{action=1, content='E', start=5, end=10}
NOTES: to get your goal you must implement a LimitedFIFOListLooking for information I've seen that Apache's kids (like not... ^_^) already has an implementation called http://commons.apache.org/proper/commons-collections/javadocs/api-release/org/apache/commons/collections4/queue/CircularFifoQueue.html .eye because it is possible to change the size, if you want to make it fixed, overwrite setSize() so I don't do anything.@Override
public void setSize(int size) {
}
I hope to have explained your doubts well and clarified, even if you have any doubt, https://www.youtube.com/watch?v=QO7KYyDSkXA :).