Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a drop-in replacement for Java Stack that is not synchronized?

I have a large codebase (written by me) that uses the Stack data structure. This was used for convenience and I am using it as Stack sometimes or Vector/List some other times.

After a performance review however it was decided that we do not want to pay extra for the synchronization safety. I need now to replace this structure with a non-synchronized one (and it is mentioned a lot of times in the code).

I was happy to discover that Apache collections includes an ArrayStack which is exactly what I want (same as Java stack but non-synchronized). However this does NOT have generics as modern Java 5 code (which is what I use). And I am not going to convert my code to look like Java 1.4

So is there any other Java 5 compliant drop-in replacement for Java Stack or do I need to write my own?

Update:

I used LinkedList with tuned "pop"/"push" methods.

like image 732
kazanaki Avatar asked Feb 16 '11 09:02

kazanaki


1 Answers

When you say "Java 5 compliant" - ArrayDeque<T> didn't arrive until Java 6, but sounds like what you're after (using the Deque<T> interface where appropriate, of course). You can use it as a stack when you want to, or a queue where that's more appropriate... just call the appropriate methods, basically.

like image 110
Jon Skeet Avatar answered Sep 23 '22 05:09

Jon Skeet