public static void main(String args[]) throws JSONException {
JSONObject json = new JSONObject();
json.put("name", "abcgdj");
json.put("no", "1234");
json.put("contact", "6748356");
Iterator<?> keys = json.keys();
System.err.println(Iterators.size(keys));
System.err.println(Iterators.size(keys));
}
In this code, after executing Iterators.size(keys)
, the iterator is becoming empty and for the second print statement, it returning 0.
The size()
method is under the package com.google.common.collect.iterators
.So I've looked the code of Iterators.size() function.It was,
public static int size(Iterator<?> iterator) {
long count = 0L;
while (iterator.hasNext()) {
iterator.next();
count++;
}
return Ints.saturatedCast(count);
}
So I have a doubt that how the iterator keys
becomes empty. whether it is called by reference?
Can anyone explain what's happening inside size() function