Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 472
Nirmal Avatar asked Jul 01 '26 09:07

Nirmal


1 Answers

You can do this:

Collections.reverse(entityStack);
like image 126
Ted Hopp Avatar answered Jul 02 '26 23:07

Ted Hopp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!