简体   繁体   中英

How memory is allocated for Stack and Queue in java

We know ArrayList uses dynamic array to store data. LinkedList uses linked list to store data. So, for both these case we know how it works when there is a new element added or deleted(Memory wise). Now similarly, how memory is allocated for a Stack or Queue in java. What happens, at Memory level, when I add/delete element to/from Stack or Queue.

Well, Queue is an interface, so not much to say about that. There's a lot of implementations and different behaviours. Anyway, for instance, ArrayBlockingQueue has a memory allocation similar to ArrayList . Anyway, there are two major groups (bounded and unbounded) but it will really depend on the implementation you choose.

About Stack , the memory allocation is also pretty similar to ArrayList as it's a subclass of Vector and this is backed by an array.

java.util.Stack is a subclass of java.util.Vector which is nothing but an array. Now java.util.Queue is an interface and has various implementations which are divided into two primary categories:

  1. bounded - backed by an underlying array of fixed size. A common example is ArrayBlockingQueue
  2. unbounded - backed by a linked list and hence theoretically infinite. A common example is LinkedBlockingQueue

Please check the docs for Stack and Queue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM