Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JAVA stack implementation

 
Balaji Ambresh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic