Java – HashSet is the best way to implement iterators that do not support remove()
•
Java
I have a class that uses HashSet. I want the class to implement iteratable, but I don't want the class iterator to support the remove () method
The default iterator for HashSet is HashSet Keyiterator, which is a private class in the HashSet class, so I can't simply extend it and override the remove method
Ideally, I want to avoid writing a wrapper class for keyiterator, but I don't know how to easily implement my own iterator in any other way
Any ideas?
Cheers!
Pete
Solution
java.util.Collections.unmodifiableSet(myHashSet).iterator();
java.util.Collections.unmodifiableSet(myHashSet).iterator();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码