Circular functionality in Java Data Structure

  • Thread starter Proton Projects - Moin
  • Start date
P

Proton Projects - Moin

Hi all,

I need to have a circular functionality.

If i construct a datastructure for size 2000.....then if i try to add
the 2001th object...it should automatically remove the first element
and adds the 2001th element to retain the size to 2000.

Can anybody help me in this regard
Thanks
Moin
 
K

Knute Johnson

Proton said:
Hi all,

I need to have a circular functionality.

If i construct a datastructure for size 2000.....then if i try to add
the 2001th object...it should automatically remove the first element
and adds the 2001th element to retain the size to 2000.

Can anybody help me in this regard
Thanks
Moin

Look at LinkedBlockingDeque. You will have to implement the removal of
the first element but that should be too complicated.
 
H

hvt

Hi all,

I need to have a circular functionality.

If i construct a datastructure for size 2000.....then if i try to add
the 2001th object...it should automatically remove the first element
and adds the 2001th element to retain the size to 2000.
>
Can anybody help me in this regard
Thanks
Moin

Hi Moin,
your required functionality can be obtained with some logic on arrays
or lists. Find the below code which implements the above logic with
array list:

import java.util.ArrayList;

public class FixQueue<T> {

public static final int QUEUE_SIZE = 2000; //Fixing the Queue Size
private ArrayList fixQueue;

public FixQueue(int queue_size2) {
fixQueue = new ArrayList<String>(QUEUE_SIZE);
}

public void add(String element){

if(fixQueue.size() == QUEUE_SIZE)
fixQueue.remove(0); // Remove first element when queue is full

//adds the element in the last position,
//when queue is full, the elements will be added at
2000th position
fixQueue.add(element);
}

public String toString(){
return fixQueue.toString();
}

public static void main(String[] args) {
FixQueue<String> queue = new FixQueue<String>(QUEUE_SIZE);
for(int i=1; i<=2010; i++)
queue.add("e"+i);

System.out.println(queue);
}
}
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Proton Projects - Moin schreef:
Hi all,

I need to have a circular functionality.

If i construct a datastructure for size 2000.....then if i try to add
the 2001th object...it should automatically remove the first element
and adds the 2001th element to retain the size to 2000.

Have a look at
http://jakarta.apache.org/commons/c...commons/collections/buffer/BoundedBuffer.html
and the Buffer package in general.

If you want a version that uses generics, contact me.

H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFF7YNFe+7xMGD3itQRArr2AJ0bzl3FtgpsHIGzok7DHncuCOoq1QCfaRQu
hAmNIuhlV9mvcVnBzpSNBPU=
=rvta
-----END PGP SIGNATURE-----
 
P

Proton Projects - Moin

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Proton Projects - Moin schreef:




Have a look athttp://jakarta.apache.org/commons/collections/apidocs/org/apache/comm...
and the Buffer package in general.

If you want a version that uses generics, contact me.

H.
- --
Hendrik Marynshttp://tcl.sfs.uni-tuebingen.de/~hendrik/
==================http://aouw.org
Ask smart questions, get good answers:http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFF7YNFe+7xMGD3itQRArr2AJ0bzl3FtgpsHIGzok7DHncuCOoq1QCfaRQu
hAmNIuhlV9mvcVnBzpSNBPU=
=rvta
-----END PGP SIGNATURE-----

Thanks all for your valuable suggestions
Moin
 
K

Karl Uppiano

Proton Projects - Moin said:
Hi all,

I need to have a circular functionality.

If i construct a datastructure for size 2000.....then if i try to add
the 2001th object...it should automatically remove the first element
and adds the 2001th element to retain the size to 2000.

Historically, these things have been implemented as "ring buffers": An array
of the appropriate size and type, with an index that is incremented modulo
the array size whenever an element is added. The compute overhead with this
type of extremely low, and no conditional logic is required at all. The
newest element simply replaces the oldest element at the insertion point.

http://en.wikipedia.org/wiki/Ring_buffer
 
K

Karl Uppiano

Karl Uppiano said:
Historically, these things have been implemented as "ring buffers": An
array of the appropriate size and type, with an index that is incremented
modulo the array size whenever an element is added. The compute overhead
with this type of extremely low, and no conditional logic is required at
all. The newest element simply replaces the oldest element at the
insertion point.

http://en.wikipedia.org/wiki/Ring_buffer

Sorry, my proofreader fell asleep. That should read: ... The compute
overhead with this approach is extremely low. ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top