How do I get the Iterator size without looping ? Is there anyway to do this ?
Iterator<String> keys = Map.keySet().iterator();
Now I want the size of Keys just forget the Map.keySet()
here.
An iterator is an implementation of java.util.Iterator interface. This interface does not provide any method to know the size of it's implementations (In fact these implementations don't need to have a size property). If you need an Iterator with a size, and a method to get it, you should build your own implementation.
However, if what you need is the size of the collection the iterator traverses, fvu answer is correct.
An Iterator just give you a method of walking over a Collection, so the "size of the Iterator" is the size of the collection it iterates over. In this case just use Set's (or any other Collection) built in size() method :
If you are in a situation where you just have access to the Iterator but need the underlying Collection's size I think you'll need to talk to the one giving you just that iterator, and either get access to the Collection, or get the size of the Collection.