Nirmal
Nirmal

Reputation: 4829

How to reverse 'java.util.stack' variable content

Variable :

Stack<Entity> entityStack = new Stack<Entity>();

It has following values :

sub_sub_sub1
sub_sub1
sub1
root1

I want these values in another stack (or list) in reverse order :

root1
sub1
sub_sub1
sub_sub_sub1

I have applied simple logic to reverse it :

for (int itr = entityStack.size()-1; itr >= 0; itr--) {
   entityStackTemp.push(entityStack.get(itr));
}

Is there any in-built method of collection to achieve this considering performance of execution ?

Upvotes: 3

Views: 834

Answers (1)

Ted Hopp
Ted Hopp

Reputation: 234867

You can do this:

Collections.reverse(entityStack);

Upvotes: 5

Related Questions