[concurrency-interest] Questions about ArrayDeque

Rémi Forax forax at univ-mlv.fr
Fri Jul 21 10:07:18 EDT 2006


Doug Lea a écrit :
> Rémi Forax wrote:
>> I have take a look to java.util.ArrayDeque today, and i have some 
>> questions :
>>
>> - Quoting the documentation :
>>   "This class is likely to be faster than {@link Stack} when used as 
>> a stack"
>>   I used to use an ArrayList to implement a stack and
>>   there is neither reference to ArrayList nor comparaison between
>>   ArrayList and ArrayDeque in the documentation
>>  
>>   Is ArrayList is faster than ArrayQueue when used as a stack ?
>>
>
> ArrayDeque should be at least as fast as ArrayList when used as pure 
> stack.
> Please tell us if you find otherwise. If nothing else, ArrayDeque has 
> more
> appropriate method names for uses as stack and queue.
Ok, else, ArrayList used toArray() in its constructor that takes a 
collection,
but ArrayDeque doesn't do the same trick, is there a reason ?

The code can be something like this one :
public ArrayDeque(Collection<? extends E> c) {
        int size=c.size();
        allocateElements(size);
        c.toArray(elements);
        tail=size;
}

>
>
>> - I've found two methods (removeFirstOccurrence() and contains())
>>    with mostly the same code.
>>   Perhaps this code could be shared between them ?
>
> It is logistically tricky to share small bits of code within
> core Java classes, since it can interfere with more important
> goal of being able to completely replace internal implementations
> of various classes over time.
Ok, it's an overall rule.

But in this peculiar case, the code that can be shared is precisely
a code related to the internal representation of ArrayDeque,
(it traverses the data structure to find the index of the first
occurence of an object).
So if the internal representation changes, this code may changed,
i don't see why it makes the class more difficult to maintain.

Is it because removeFirstOccurrence() is a method of Deque
and contains() a method of  Collection and you want
to keep them not linked.

>
> -Doug
>
>
Rémi Forax



More information about the Concurrency-interest mailing list