我想转换这个用java做...而()到Java 8do ... while()使用Java 8流?
private static final Integer PAGE_SIZE = 200;
int offset = 0;
Page page = null;
do {
// Get all items.
page = apiService.get(selector);
// Display items.
if (page.getEntries() != null) {
for (Item item : page.getEntries()) {
System.out.printf("Item with name '%s' and ID %d was found.%n", item.getName(),
item.getId());
}
} else {
System.out.println("No items were found.");
}
offset += PAGE_SIZE;
selector = builder.increaseOffsetBy(PAGE_SIZE).build();
} while (offset < page.getTotalNumEntries());
此代码API调用apiService
和检索数据。然后,我想循环直到偏移量小于totalNumberEntries
。
什么是使用while()
或foreach with step
或any other kind of loop
循环,我不知道totalNumberEntries
未做API调用(这是在循环内完成),禁止我。
我能想到的一个选择是进行API调用以获取totalNumberEntries
并继续循环。
'做{...}而(...)'是完全合法Java 8. –
请分享你已经尝试过的 – sidgate
从技术上讲,函数的方式将是Java 9中存在的'takeWhile' http://stackoverflow.com/a/32304570/1743880 – Tunaki