GeeCON Prague 2014*
The moose likes Performance and the fly likes JAVA stack implementation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Performance
Bookmark "JAVA stack implementation" Watch "JAVA stack implementation" New topic
Author

JAVA stack implementation

Balaji Ambresh
Greenhorn

Joined: Nov 08, 2006
Posts: 7
Hi,
The Stack under java.util extends Vector instead of a LinkedList.
Vector is a resizable array. So, from what I know, it should make it heavy on memory management.
Also, we’re interested only on the top element of the stack. By using an linked list, we can get away with just one pointer.

So, why does Stack extend Vector?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Because Stack and Vector were part of Java 1.0; LinkedList came along many years later. The "List" interface was added to Vector at that time.


[Jess in Action][AskingGoodQuestions]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12792
    
    5
Vector is a resizable array. So, from what I know, it should make it heavy on memory management.


Where did you get that idea? If you get the initial size guess right, the underlying Vector will never be resized.

LinkedList will always involve extra object (Entry) creation - see the source code.

Bill

Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Where did you get that idea? If you get the initial size guess right, the underlying Vector will never be resized.


He means that the Vector implementation uses an array.

Yes it is considered a design flaw since Vector's methods are exposed for an underlying Stack implementation.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
 
GeeCON Prague 2014
 
subject: JAVA stack implementation