Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any Java Iterators that close automatically (like C#)?

I'm from a C# background where the iterators implement IDisposable interface. Are there any iterators in Java that implement a similar interface that makes them close automatically?

like image 742
batman Avatar asked Dec 21 '25 11:12

batman


1 Answers

Java's Iterator is just an abstraction for iterating over some collection of objects. If the underlying data structure needs some clean-up after iteration (e.g. closing a file), then you need to keep a reference to that underlying object so you can do whatever must be done to "dispose" of it.

like image 95
DaoWen Avatar answered Dec 23 '25 00:12

DaoWen