What is the best way to create some construction with exactly five elements and when I try to put another one to remove the firstone etc ?
Asked
Active
Viewed 149 times
-1
-
2Where ? When ? What ? Which Element ? – Suresh Atta Jul 29 '14 at 12:36
-
Elements should be simple double values – Boris Pawlowski Jul 29 '14 at 12:36
-
And why can't you do it? – Eypros Jul 29 '14 at 12:38
-
Use a List and prevent the maximum length from exceeding 5. – DwB Jul 29 '14 at 12:38
-
Im wonderoing what construction should I use ? ArrayList or HashMap witch is the best way to handle it ? – Boris Pawlowski Jul 29 '14 at 12:38
-
Any of the lists would do (probably an arraylist) or an array itself. Then wrap it in an object that performs the function you desire – Richard Tingle Jul 29 '14 at 12:40
-
1people keep complaining that we are rude, but there you go. What can we understand from this question? We need a "Mind reading" functionality in this site – Sleiman Jneidi Jul 29 '14 at 12:40
-
Possible duplicate of [Java - Ring Buffer](http://stackoverflow.com/questions/7266042/java-ring-buffer) – DavidPostill Jul 29 '14 at 12:44
-
1@SleimanJneidi 6 answers, still counting :) – Suresh Atta Jul 29 '14 at 12:45
-
You want a FIFO structure of at most ( elements. Try reading this: http://stackoverflow.com/questions/9580457/fifo-class-in-java – StephaneM Jul 29 '14 at 12:45
-
Try LinkedHashMap with overriding removeEldestEntry method. – DmitryKanunnikoff Jul 29 '14 at 14:06
6 Answers
0
Queue is the LIFO object you need.
- If you are a Apache fan, you have CircularFifoQueue
- If you are more into Guava, you have EvictingQueue
- You can also do it yourself: (LinkedList is one implementation amongst others)
`
// Create
Queue q = new LinkedList();
// Insert
void safeInsert(Queue q, Object elt) {
if (q.size()>=5) {
q.poll();
}
q.offer(elt);
}

Vincent Mimoun-Prat
- 28,208
- 16
- 81
- 124
0
The CircularFifoQueue provided by Apache Commons Collections does what you need.

Brent Worden
- 10,624
- 7
- 52
- 57
0
What you are asking for is called a circular buffer or ring buffer.
The Java library does not provide an implementation for you (confirmed, confirmed again), so you will have to write one yourself, or use a third-party library.
0
I am not sure whether this is the "best solution", but it's quite simple and does exactly what you want:
List<Foo> list = new ArrayList<>();
...
list.add(item);
if(list.size()>5){
list.remove(0);
}

achingfingers
- 1,827
- 5
- 24
- 48
0
public class MyArrayList extends ArrayList<Object>{
private static final int MAX_SIZE = 5;
@Override
public void add(int index, Object object) {
if(this.size() == MAX_SIZE)
this.remove(0);
super.add(MAX_SIZE -1, object);
}
@Override
public boolean add(Object object) {
if(this.size() == MAX_SIZE)
this.remove(0);
return super.add(object);
}
@Override
public boolean addAll(Collection<? extends Object> collection) {
throw new SomeException();
}
@Override
public boolean addAll(int index, Collection<? extends Object> collection) {
throw new SomeException();
}
}