Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Queue vs Stack

Tags:

java

android

Why does java.util.Stack allows me to create a new Stack in an android activity with a simple constructor like:

Stack < ImageView> stack = new Stack< ImageView>();

and i cannot do the same with java.util.Queue? Shouldn't a queue have a similar constructor? Strange enough on http://developer.android.com/reference/java/util/Stack.html it says the Stack has a public constructor and on http://developer.android.com/reference/java/util/Queue.html i don't see a similar constructor for a queue.. why is that? what is the way to have a Queue of ImageView elements for example? Thanks.

like image 431
user1555863 Avatar asked Aug 29 '12 14:08

user1555863


People also ask

Is queue or stack faster?

Both queue and stack seem fine for this purpose, as the order in which they are taken out does not matter. The question is: is any of them significantly faster than the other? Answer: most probably not. Best answer: try both and test it.


2 Answers

Because Queue is an interface, you should initial it with a LinkedList:

Queue<String> qe = new LinkedList<String>();

qe.add("b");
qe.add("a");
qe.add("c");

//Traverse queue
Iterator it = qe.iterator();

System.out.println("Initial Size of Queue :" + qe.size());

while(it.hasNext())
{
   String iteratorValue = (String) it.next();
   System.out.println("Queue Next Value :" + iteratorValue);
}
like image 142
ductran Avatar answered Oct 20 '22 20:10

ductran


It is because Queue is just an interface. To create a Queue object you need a class, what implements the methods of a Queue.

Some actual implementations of a Queue: link

like image 22
Balázs Édes Avatar answered Oct 20 '22 21:10

Balázs Édes