Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Enumeration vs Iteration vs ForLoop [closed]

What is the difference between (Enumeration, Iterator, Loop) ??

In the following code each example (Enumeration, Iterator, Loop) gets the same output ] but I need to know what is the real difference between them? Is there specific case where i should use one of them and cant use the others? any advantages in one of them over the others?

like image 247
Hanaa Gaafar Avatar asked Nov 20 '25 22:11

Hanaa Gaafar


1 Answers

Enumeration VS Iterator

Iterator is mainly used with non Thread safe Collection. Iterator is designed in such a way that it fails whenever concurrent modification are made to the Collections i.e if a collection which is made for single Threaded environment is used in multithreaded environment and changes are made to it by Thread while it is being traversed then the traversing fails.

Enumeration is used only with Thread safe Collection. When such a collection is used in multithreaded environment consistency of the data is guarantee hence Enumeration never fails

For better understanding you should their Internal Working........

A Collection and its Iterator and Enumeration has a stronger HAS-A relation between them i.e Existence of both parties depends upon each other.

like image 132
Yogesh Avatar answered Nov 23 '25 12:11

Yogesh



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!