The reply to this query is so simple as that, you have to be utilizing Iterator’s take away() technique to delete any object from Assortment you’re iterating, however this isn’t the top of this query.
Almost certainly you may be requested to clarify, what’s a distinction in eradicating objects utilizing the take away() technique of Assortment over take away() technique of Iterator and why one ought to use over the opposite?
The reason being ConcurrentModificationException, should you use the take away() technique of Checklist, Set, or principally from any Assortment to delete object whereas iterating, it would throw ConcurrentModificationException.
Although take away() technique of java.util.Assortment works superb to take away particular person objects, they do not work properly if you find yourself iterating over a set. Let’s have a look at a code instance to clear doubts
And, If you’re new to the Java world then I additionally advocate you undergo The Full Java MasterClass on Udemy to study Java in a greater and extra structured means. This is likely one of the finest and up-to-date programs to study Java on-line.
Learn how to Take away components From ArrayList whereas Iterating? Instance
Within the beneath code, we’ve got a listing of exchanges and we’re eradicating exchanges which might be closed in the mean time. Code is so simple as it could possibly be and it is arduous to seek out something incorrect with it by simply trying, however issues will probably be totally different if you run it.
You’ll be hit by ConcurrentModificationException, as quickly as you run, as a result of listed here are we’re utilizing the take away() technique of ArrayList to take away objects, as a substitute of the Iterator’s take away technique. To be able to repair ConcurrentModificationException, simply use the take away() technique of java.util.Iteator class.
That is why generally interviewers current the code and asks you to seek out bugs on it, or they might merely ask you to jot down code for eradicating objects from Assortment whereas traversing over them. All the time bear in mind to make use of Iterator’s take away() technique for eradicating objects from Assortment in Java.
Different Java Assortment tutorials you might like
- Learn how to kind a Map by keys and values in Java? (tutorial)
- Learn how to kind an ArrayList in ascending and descending order in Java? (tutorial)
- What’s the distinction between ArrayList and HashSet in Java? (reply)
- What’s the distinction between TreeMap and TreeSet in Java? (reply)
- What’s the distinction between HashMap and ConcurrentHashMap in Java? (reply)
- The distinction between HashSet and TreeSet in Java? (reply)
- The distinction between ArrayList and LinkedList in Java? (reply)
- The distinction between Vector and ArrayList in Java? (reply)
Thanks for studying this text to date. If you happen to like this text then please share it with your folks and colleagues. When you have any questions or suggestions then please drop a remark.