What's the difference b/w next() and hasNext()?

jamesgo
Giga Contributor

if the query has only one match, will hasNext() return true?

7 REPLIES 7

Brad Tilton
ServiceNow Employee
ServiceNow Employee

hasNext() either returns true or false while next() will actually iterate to the record.



So I could do:


gr.query();


if (gr.next()) {


  gr.field = 'value';


  gr.update();


}


but this won't update anything:


gr.query();


if (gr.hasNext()) {


  gr.field = 'value';


  gr.update();


}



So if there are one or more records contained in your gr set, hasNext() will return true.


Hi Brad, in 2nd scenario, gr doesn't iterate to the actual record? if there is only one match, what is contained in the gr when using hasNext?


When I was try with above code in nest() if I got the updated record but not updated in case of hasNext().

kumaril1
Giga Expert

hasNext() : hasNext() method returns true if iterator have more elements.


next() : next() method returns the next element and also moves to the next element.