GeezerVison is showing github.com — Iterator.next() is ignored for List<Value<T>> · Issue #993 · googleapis/google-cloud-java
Iterator.next() is ignored for List<Value<T>> #993DescriptionThis code produces an endless loop:
List<Value<Long>> releases = entity.getList("releases");
System.out.println(releases.size()); // prints 1
while (releases.iterator().hasNext()) {
Value<Long> value = releases.iterator().next();
System.out.println(value.get()); // prints correct value over and over
}
Activityajkannan commented on May 6, 2016 The condition in the while loop creates a new iterator for each iteration of the loop, which causes the infinite loop. If you save the iterator you create before the while loop rather than creating a new one each time, the problem should be fixed. volgin commented on May 6, 2016 Is this documented? Typically I don't expect a new iterator created when I simply pass a reference to it.
-----Original Message-----
From: "Ajay Kannan" [email protected]
Sent: 5/6/2016 6:57 PM
To: "GoogleCloudPlatform/gcloud-java" [email protected]
Cc: "volgin" [email protected]; "Author" [email protected]
Subject: Re: [GoogleCloudPlatform/gcloud-java] Iterator.next() is ignored forList<Value> (#993)
The condition in the while loop creates a new iterator for each iteration of the loop, which causes the infinite loop. If you save the iterator you create before the while loop rather than creating a new one each time, the problem should be fixed.
—
You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub ajkannan commented on May 6, 2016 releases is of type java.util.List (which is Iterable). The contract of Iterable's iterator() is to return a new iterator over all items in the list. The code you provide would also loop endlessly if you used some other generic type (e.g. Integer), like below:
List<Integer> releases = Collections.singletonList(1);
System.out.println(releases.size());
while (releases.iterator().hasNext()) {
int value = releases.iterator().next();
System.out.println(value);
}
volgin commented on May 6, 2016 OK. Thank you for a quick response, and feel free to close this issue.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|