java - Collections with limited size -
this question has answer here:
- define fixed-size list in java 7 answers
is there collection allows me limit number of elements allowed?
what need collection size of 5 elements, when collection full, new element can added, oldest element of collection replaced new element.
you can extend old arraylist (or other implementation fits best).
import java.util.arraylist; public class limitedcollection<e> extends arraylist<e> { public static final int max_elements = 2; @override public boolean add(e e) { if (this.size() < max_elements) { return super.add(e); } else { return false; } } }
Comments
Post a Comment