Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Why doesn't ArrayDeque implement the List interface?

Reply
Thread Tools

Why doesn't ArrayDeque implement the List interface?

 
 
Joe Gottman
Guest
Posts: n/a
 
      03-04-2009
I have an application where I need to access the elements of a
container by index and add and remove elements at either end. I thought
that ArrayDeque was the obvious solution, but I was surprised to find
that it doesn't implement the List interface. Why is this? My use case
isn't unusual; it's the same as the C++ deque template. It's especially
galling in that it would be possible to implement a get() member
function with constant time complexity in ArrayDeque, but the LinkedList
class, which does implement the List() interface, requires linear time
to perform get(). It seems backward to me that Java mandates the less
efficient data structure for my use case.

Joe Gottman
 
Reply With Quote
 
 
 
 
Mark Space
Guest
Posts: n/a
 
      03-04-2009
Joe Gottman wrote:
> I have an application where I need to access the elements of a
> container by index and add and remove elements at either end. I thought
> that ArrayDeque was the obvious solution, but I was surprised to find
> that it doesn't implement the List interface. Why is this? My use case



Probably be inserting elements at the head of an ArrayDeque would give
very poor performance. Look at LinkedList instead, it implements both
List and Deque.
 
Reply With Quote
 
 
 
 
Joe Gottman
Guest
Posts: n/a
 
      03-04-2009
Mark Space wrote:
Mark Space wrote:
> Joe Gottman wrote:
>> I have an application where I need to access the elements of a
>> container by index and add and remove elements at either end. I
>> thought that ArrayDeque was the obvious solution, but I was surprised
>> to find that it doesn't implement the List interface. Why is this?
>> My use case

>
>
> Probably be inserting elements at the head of an ArrayDeque would give
> very poor performance. Look at LinkedList instead, it implements both
> List and Deque.


> Joe Gottman wrote:
>> I have an application where I need to access the elements of a
>> container by index and add and remove elements at either end. I
>> thought that ArrayDeque was the obvious solution, but I was surprised
>> to find that it doesn't implement the List interface. Why is this?
>> My use case

>
>
> Probably be inserting elements at the head of an ArrayDeque would give
> very poor performance. Look at LinkedList instead, it implements both
> List and Deque.


Check the documentation of ArrayDeque
(http://java.sun.com/javase/6/docs/ap...rayDeque.html). It's
faster than LinkedList for inserting and deleting at either end. It
would also be faster than LinkedList for element access, if only the
get() function were provided.



Joe Gottman
 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      03-04-2009
Joe Gottman wrote:
>
> Check the documentation of ArrayDeque
> (http://java.sun.com/javase/6/docs/ap...rayDeque.html). It's
> faster than LinkedList for inserting and deleting at either end.
> It would also be faster than LinkedList for element access, if only
> the get() function were provided.


Good question; get() would be trivial to code in the current ArrayDeque
implementation.


 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      03-04-2009
Joe Gottman wrote:
> Check the documentation of ArrayDeque
> (http://java.sun.com/javase/6/docs/ap...rayDeque.html). It's
> faster than LinkedList for inserting and deleting at either end. It
> would also be faster than LinkedList for element access, if only the
> get() function were provided.


If I had some ham I could have a ham and cheese sandwich, if I only had some
cheese.

This is one of those rare cases when Sun hasn't done your job for you,
necessitating that one implement the desired functionality oneself.

You asked upthread why ArrayDeque doesn't do what you want. I guess Sun just
wasn't paying you close enough attention when they wrote their API.

--
Lew
 
Reply With Quote
 
Joe Gottman
Guest
Posts: n/a
 
      03-04-2009
Lew wrote:
> Joe Gottman wrote:
>> Check the documentation of ArrayDeque
>> (http://java.sun.com/javase/6/docs/ap...rayDeque.html).
>> It's faster than LinkedList for inserting and deleting at either end.
>> It would also be faster than LinkedList for element access, if only
>> the get() function were provided.

>
> If I had some ham I could have a ham and cheese sandwich, if I only had
> some cheese.
>
> This is one of those rare cases when Sun hasn't done your job for you,
> necessitating that one implement the desired functionality oneself.
>
> You asked upthread why ArrayDeque doesn't do what you want. I guess Sun
> just wasn't paying you close enough attention when they wrote their API.
>


Is there a procedure for requesting this functionality be added?

Joe Gottman
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      03-04-2009
Joe Gottman wrote:
> Is there a procedure for requesting this functionality be added?


Actually, you may be stuck with LinkedList.

It could be that ArrayDeque can't be all things to all your desired interfaces
with all the performance that you want, because it is contradictory to want
constant (amortized) access time for 'get()' and for the other operations for
which you want fast action.

Joe Gottman wrote:
>> It's especially galling ...
>> It seems backward to me that Java mandates the less efficient
>> data structure for my use case.


Since it's your use case, maybe you should write the code.

--
Lew
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      03-04-2009
Joe Gottman wrote:

> Check the documentation of ArrayDeque
> (http://java.sun.com/javase/6/docs/ap...rayDeque.html). It's
> faster than LinkedList for inserting and deleting at either end. It



It actually doesn't say that. It says "faster than Stack" ... Stack is
synchronized. It says "faster than LinkedList when used as a Queue" ...
Queues are inserted at the end and removed from the head. That's not
inserting and deleting at either end.

The latter makes me wonder if ArrayDeques are implemented as circular
buffers. This might mean that the "offset" which get() would use is
likely to shift around. That offset also might change completely if the
underlying array fills up and has to be copied to a larger array.
(ArrayDeques are specified to grow as needed, they don't block or throw
errors related to being out of storage.)


> would also be faster than LinkedList for element access, if only the
> get() function were provided.



I think if you try implementing your own you'll find out exactly what
the issue is. I'll bet it's nasty too.
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      03-04-2009
Mark Space wrote:
> Joe Gottman wrote:
>
>> Check the documentation of ArrayDeque
>> (http://java.sun.com/javase/6/docs/ap...rayDeque.html).
>> It's faster than LinkedList for inserting and deleting at either end. It

>
>
> It actually doesn't say that. It says "faster than Stack" ... Stack is
> synchronized. It says "faster than LinkedList when used as a Queue" ...
> Queues are inserted at the end and removed from the head. That's not
> inserting and deleting at either end.
>
> The latter makes me wonder if ArrayDeques are implemented as circular
> buffers. This might mean that the "offset" which get() would use is
> likely to shift around. That offset also might change completely if the
> underlying array fills up and has to be copied to a larger array.
> (ArrayDeques are specified to grow as needed, they don't block or throw
> errors related to being out of storage.)
>
>
>> would also be faster than LinkedList for element access, if only the
>> get() function were provided.


I'm thinking you might actually have to create your own low-level
implementation, in order to get the functionality you desire. That is
unfortunate, but sometimes is the case.
>
>
> I think if you try implementing your own you'll find out exactly what
> the issue is. I'll bet it's nasty too.


It seems to me that you could pretty easily create a ring-buffer that
had a List style get/set, which was reasonably defined to consider the
get/set index as an offset from the buffer. An implementation wouldn't
be terribly difficult.


--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      03-04-2009
Lew wrote:
> Joe Gottman wrote:
>> Is there a procedure for requesting this functionality be added?

>
> Actually, you may be stuck with LinkedList.
>
> It could be that ArrayDeque can't be all things to all your desired
> interfaces with all the performance that you want, because it is
> contradictory to want constant (amortized) access time for 'get()'
> and for the other operations for which you want fast action.


Not in this case, though. Keep the current implementation, and get()
would be about five lines of code with no looping required.
(ArrayDeque is implemented as a segment of an an array, which might
wrap when it reaches the array end. Once you know the current head
and the current tail, finding member "n" is roughly ((head + n) mod
arraySize).)


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to implement two drop-down list dependency in ASP/HTML/VBscript forms? srini HTML 7 08-30-2010 12:49 PM
Why does list.__getitem__ return a list instance for subclasses ofthe list type? dackz Python 0 02-06-2007 04:44 PM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Collections.sort - do i *have* to implement List timasmith@hotmail.com Java 4 06-01-2006 04:36 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57