java - Collections with limited size -


this question has answer here:

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

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -