tag:blogger.com,1999:blog-8712770457197348465.post4898863838273135873..comments2024-03-18T23:01:42.768-07:00Comments on Javarevisited: What is difference between Enumeration and Iterator in Java? Answerjavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger24125tag:blogger.com,1999:blog-8712770457197348465.post-31140489755716171612017-02-23T00:09:43.527-08:002017-02-23T00:09:43.527-08:00This article helped me a lot especially the thread...This article helped me a lot especially the thread saftey part. Thank a lot :)Anonymoushttps://www.blogger.com/profile/16654059855993325135noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-49828637078921448092016-09-04T02:10:22.328-07:002016-09-04T02:10:22.328-07:00@Javin: Thanks a lot :)@Javin: Thanks a lot :)Anonymoushttps://www.blogger.com/profile/01057630465826314764noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-12582961452785718812015-12-31T22:53:13.104-08:002015-12-31T22:53:13.104-08:00@Navneet, Before each iteration Iterator checks wh...@Navneet, Before each iteration Iterator checks whether a collection is modified concurrently by another thread or not using modCount and throws ConcurrentModificationException if collection is modified after iteration begins, except via Iterator methods. Enumeration doesn't do that kind of check, hence cannot detect such errors. This is why I called Iterator more safe and secure in javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-145030195651447132015-12-31T10:20:10.044-08:002015-12-31T10:20:10.044-08:00Hi,
Great Article.
I want to ask 1 thing, you have...Hi,<br />Great Article.<br />I want to ask 1 thing, you have written "Also Iterator is more secure and safe as compared to Enumeration". My question is, if enumeration do not allow modification( as read only ) & iterator does, then how the iterator is thread safe. Isn't be the enumerator is more safe to use ? Please do let me know.<br />Thanks in advance. Anonymoushttps://www.blogger.com/profile/01057630465826314764noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-62693978991132623712015-06-08T09:58:18.060-07:002015-06-08T09:58:18.060-07:00You said "where as by using Iterator we can m...You said "where as by using Iterator we can manipulate the objects like adding and removing the objects from collection e.g. Arraylist", but if you add elements while iterating it throws "concurrentModificationException"Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-79356128947565044172014-03-28T21:11:33.434-07:002014-03-28T21:11:33.434-07:00Hello @Anonymous, Thanks for your comments. Actual...Hello @Anonymous, Thanks for your comments. Actually iterator() method comes from java.lang.Iterable interface, all Collection classes implements this interface, which also allows them to be used as target of "foreach" loop. There is no similar interface for Enumeration, as you know its convention to return Enumeration by calling elements() method, which you see in Vector and other javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-17839792347265214752014-03-28T12:20:17.477-07:002014-03-28T12:20:17.477-07:00Hi javin... i like reading your blogs.. but i hav...Hi javin... i like reading your blogs.. but i have little doubt here.<br />1. as Iterator interface can iterate on any collection with an iterator method which returns us Iterator type object.<br /> do we have any method for Enumeration Interface.???? <br />or we can traverse through only vector which has element method.???? i'm confused<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-6095812161425627722013-10-01T16:39:54.641-07:002013-10-01T16:39:54.641-07:00ConcurrentModificationException has nothing to do ...ConcurrentModificationException has nothing to do with thread safety according to a stackoverflow reply I recently read... just_a_guyhttps://www.blogger.com/profile/14601097418443078026noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-17866714977485693622012-05-31T04:57:13.213-07:002012-05-31T04:57:13.213-07:00is iterator synchronized?is iterator synchronized?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-9702470797805933852012-05-29T05:48:14.551-07:002012-05-29T05:48:14.551-07:00One more diff is that next() in Iterator doesnt ne...One more diff is that next() in Iterator doesnt need any casting where as nextElement() in Enumeration returns Object and hence casting is required.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-83053489645518348932012-05-21T02:04:31.994-07:002012-05-21T02:04:31.994-07:00One important point to note about Iterator's r...One important point to note about Iterator's remove method is that it will throw IllegalStateException if you call remove() without calling next() method of Iterator. Also you can only call remove() method one time in one iteration. Things gets more tricky when you are using ListIterator where call to remove can only be add after calling next() or prev() and without any call to add() or Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-55905232986526313082012-05-08T23:45:24.243-07:002012-05-08T23:45:24.243-07:00Sir, i have a question, in your blog, you are aski...Sir, i have a question, in your blog, you are asking that "Also Iterator is more secure and safe as compared to Enumeration because it does not allow other thread to modify the collection object while some thread is iterating over it and throws ConcurrentModificationException." And with this point, you decide whether you are going to use Iterator or Enumeration, but as you mention thatAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-7760065845781071072012-04-10T06:17:10.943-07:002012-04-10T06:17:10.943-07:00Hi Vijay which kind of Map are you using ? You nee...Hi Vijay which kind of Map are you using ? You need to make sure no thread modify Map while you are iterating over it, if its not a ConcurrentHashMap and only use Iterator.remove() method for removing items from Map.Javin @ iterate map in javahttp://javarevisited.blogspot.com/2011/12/how-to-traverse-or-loop-hashmap-in-java.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-55598540059128078172012-04-09T10:01:30.124-07:002012-04-09T10:01:30.124-07:00Hi Paul, I have a requirement where i need to tra...Hi Paul, I have a requirement where i need to traverse through the Map and change the value objects in it on some condition.... but i found concurrentmodification exception..... how do i achieve to satisfy my req....Vijay Rhttps://www.blogger.com/profile/17862972370501524349noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-26566515931279506332012-01-06T05:16:01.869-08:002012-01-06T05:16:01.869-08:00I really enjoy reading your blogs. Your lets me di...I really enjoy reading your blogs. Your lets me dive deeply into the concepts but in very easy way.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-39409320743311921212011-12-05T20:37:30.402-08:002011-12-05T20:37:30.402-08:00Iterator vs Enumeration is very old concept, I am ...Iterator vs Enumeration is very old concept, I am not sure why people still ask Enumeration vs iterator, difference between iterator and enumeration, these questions doesn't make any sense of world of advanced Java and highly concurrent and scalable system's world.Still Iterator vs Enumeration .....noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-65836461946576350862011-11-24T04:03:38.479-08:002011-11-24T04:03:38.479-08:00Iterator is synchronized and enumeration is not.Iterator is synchronized and enumeration is not.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-25147119596077016482011-11-14T23:27:39.331-08:002011-11-14T23:27:39.331-08:00@Anonymous, your Guess is write Iterator has bette...@Anonymous, your Guess is write Iterator has better performance than Enumeration and use of Iterator is recommended over Enumeration in Java.Rajiv Goelnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-62604944667927298912011-11-07T07:14:01.659-08:002011-11-07T07:14:01.659-08:00Can you please explain What is difference between ...Can you please explain What is difference between HashMap iterator and ArrayList Iterator , thanks ?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-18606576527506492902011-07-27T02:25:41.976-07:002011-07-27T02:25:41.976-07:00In terms of performance which one is better betwee...In terms of performance which one is better between Enumeration and Iterator that could be the real big difference between Iterator and Enumeration. my guess is on Iterator because its new one and Enumeration is older one.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-40587729626974370492011-07-21T07:38:26.028-07:002011-07-21T07:38:26.028-07:00hi Anonymous Iterator vs Enumeration is indeed a c...hi Anonymous Iterator vs Enumeration is indeed a common question glad you find my blog post about iterator and enumeration useful.Javin @ tibco tutorials for beginnershttp://javarevisited.blogspot.com/2010/10/tibco-rv-messagging.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-28390717642859143102011-07-21T01:27:29.464-07:002011-07-21T01:27:29.464-07:00This same interview question "What is differe...This same interview question "What is difference between Iterator and Enumeration is asked to one of my friend Rashmi which has appeared in a java interview. thanks dude.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-81860823069507915122011-06-11T22:23:02.760-07:002011-06-11T22:23:02.760-07:00Hey nice1,
B in touch so that we can discuss some ...Hey nice1,<br />B in touch so that we can discuss some java things,<br />Bcoz u hav lots of working experience(7years) that can help me a lot,bhabani pattanayakhttps://www.blogger.com/profile/09650249063755739808noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-44334913819253635862011-02-05T04:57:45.391-08:002011-02-05T04:57:45.391-08:00Thanks Jeune. you liked the post this is common in...Thanks Jeune. you liked the post this is common interview question in java and I wanted to keep it short and simple.Javin @ Tibco RV Tutorialhttp://javarevisited.blogspot.comnoreply@blogger.com