66import java .math .BigDecimal ;
77import java .util .List ;
88import java .util .Map ;
9+ import java .util .NoSuchElementException ;
910
1011/**
1112 * Decorates {@code Iterable<T>} objects to enable use of Linq like expressions.
@@ -225,48 +226,131 @@ public interface Stream<T> extends Iterable<T> {
225226 public int count ();
226227
227228 /**
228- * Returns the first element of a sequence.
229+ * Returns the first element of a sequence; this method throws an exception if the sequence is empty .
229230 *
230231 * @return The first element in the specified sequence.
231- * @throws java.util. NoSuchElementException if the sequence is empty.
232+ * @throws NoSuchElementException if the sequence is empty.
232233 */
233234 public T first ();
234235
235236 /**
236- * Returns the first element in a sequence that satisfies a specified condition.
237+ * Returns the first element in a sequence that satisfies the specified condition; this method throws
238+ * an exception if no element in the sequence satisfies the condition.
237239 *
238240 * @param predicate A function to test each element for a condition.
239241 * @return The first element in the sequence that passes the test in the specified predicate function.
240- * @throws java.util. NoSuchElementException if no element matches the sequence.
242+ * @throws NoSuchElementException if no element in the sequence satisfies the condition .
241243 */
242244 public T first (Predicate <T > predicate );
243245
244246 /**
245- * Returns the last element of a sequence.
247+ * Returns the first element of a sequence, or null if empty.
248+ *
249+ * @return The first element in the specified sequence, or null if empty.
250+ */
251+ public T firstOrNull ();
252+
253+ /**
254+ * Returns the first element in a sequence that satisfies a specified condition, or null if none does.
255+ *
256+ * @param predicate A function to test each element for a condition.
257+ * @return The first element in the sequence that passes the test in the specified predicate function, or null if none does.
258+ */
259+ public T firstOrNull (Predicate <T > predicate );
260+
261+ /**
262+ * Returns the first element of a sequence, or defaultValue if empty.
263+ *
264+ * @return The first element in the specified sequence, or defaultValue if empty.
265+ */
266+ public T firstOrDefault (T defaultValue );
267+
268+ /**
269+ * Returns the first element in a sequence that satisfies a specified condition, or defaultValue if none does.
270+ *
271+ * @param predicate A function to test each element for a condition.
272+ * @return The first element in the sequence that passes the test in the specified predicate function, or or defaultValue if none does.
273+ */
274+ public T firstOrDefault (Predicate <T > predicate , T defaultValue );
275+
276+ /**
277+ * Returns the last element of a sequence; this method throws an exception if the sequence is empty.
246278 *
247279 * @return The last element in the specified sequence.
248- * @throws java.util. NoSuchElementException if the sequence is empty.
280+ * @throws NoSuchElementException if the sequence is empty.
249281 */
250282 public T last ();
251283
252284 /**
253- * Returns the last element in a sequence that satisfies a specified condition.
285+ * Returns the last element in a sequence that satisfies the specified condition; this method throws
286+ * an exception if no element in the sequence satisfies the condition.
254287 *
255288 * @param predicate A function to test each element for a condition.
256289 * @return The last element in the sequence that passes the test in the specified predicate function.
257- * @throws java.util. NoSuchElementException if no element matches the sequence.
290+ * @throws NoSuchElementException if no element in the sequence satisfies the condition .
258291 */
259292 public T last (Predicate <T > predicate );
260293
261294 /**
262- * Returns the only element of a sequence, or a default value if the sequence is empty; this method throws
263- * an exception if there is more than one element in the sequence.
295+ * Returns the last element of a sequence, or null if empty.
296+ *
297+ * @return The last element in the specified sequence, or null if empty.
298+ */
299+ public T lastOrNull ();
300+
301+ /**
302+ * Returns the last element in a sequence that satisfies a specified condition, or null if none does.
303+ *
304+ * @param predicate A function to test each element for a condition.
305+ * @return The last element in the sequence that passes the test in the specified predicate function, or null if none does.
306+ */
307+ public T lastOrNull (Predicate <T > predicate );
308+
309+ /**
310+ * Returns the last element of a sequence, or defaultValue if empty.
311+ *
312+ * @return The last element in the specified sequence, or defaultValue if empty.
313+ */
314+ public T lastOrDefault (T defaultValue );
315+
316+ /**
317+ * Returns the last element in a sequence that satisfies a specified condition, or defaultValue if none does.
318+ *
319+ * @param predicate A function to test each element for a condition.
320+ * @return The last element in the sequence that passes the test in the specified predicate function, or or defaultValue if none does.
321+ */
322+ public T lastOrDefault (Predicate <T > predicate , T defaultValue );
323+
324+ /**
325+ * Returns the only element of a sequence; this method throws an exception if there is more than
326+ * one element in the sequence or if the sequence is empty.
264327 *
265328 * @return The single element of the input sequence, or default(TSource) if the sequence contains no elements.
266329 * @throws MultipleElementsFoundException The input sequence contains more than one matching element.
330+ * @throws NoSuchElementException If the sequence is empty.
267331 */
268332 public T single () throws MultipleElementsFoundException ;
269333
334+ /**
335+ * Returns the only element of a sequence that satisfies a specified condition; this method throws an exception
336+ * if multiple elements in the sequence satisfy the condition or if none does.
337+ *
338+ * @param predicate A function to test an element for a condition.
339+ * @return The single element of the input sequence that satisfies the condition, or null if no such element is found.
340+ * @throws MultipleElementsFoundException The input sequence contains more than one matching element.
341+ * @throws NoSuchElementException If no element in the sequence satisfies the condition.
342+ */
343+ public T single (Predicate <T > predicate ) throws MultipleElementsFoundException ;
344+
345+ /**
346+ * Returns the only element of a sequence, or a null if the sequence is empty; this method throws
347+ * an exception if there is more than one element in the sequence.
348+ *
349+ * @return The single element of the input sequence, or default(TSource) if the sequence contains no elements.
350+ * @throws MultipleElementsFoundException The input sequence contains more than one matching element.
351+ */
352+ public T singleOrNull ();
353+
270354 /**
271355 * Returns the only element of a sequence that satisfies a specified condition or null if
272356 * no such element exists; this method throws an exception if more than one element satisfies the condition.
@@ -275,7 +359,26 @@ public interface Stream<T> extends Iterable<T> {
275359 * @return The single element of the input sequence that satisfies the condition, or null if no such element is found.
276360 * @throws MultipleElementsFoundException The input sequence contains more than one matching element.
277361 */
278- public T single (Predicate <T > predicate ) throws MultipleElementsFoundException ;
362+ public T singleOrNull (Predicate <T > predicate );
363+
364+ /**
365+ * Returns the only element of a sequence, or a defaultValue if the sequence is empty; this method throws
366+ * an exception if there is more than one element in the sequence.
367+ *
368+ * @return The single element of the input sequence, or default(TSource) if the sequence contains no elements.
369+ * @throws MultipleElementsFoundException The input sequence contains more than one matching element.
370+ */
371+ public T singleOrDefault (T defaultValue );
372+
373+ /**
374+ * Returns the only element of a sequence that satisfies a specified condition or defaultValue if
375+ * no such element exists; this method throws an exception if more than one element satisfies the condition.
376+ *
377+ * @param predicate A function to test an element for a condition.
378+ * @return The single element of the input sequence that satisfies the condition, or null if no such element is found.
379+ * @throws MultipleElementsFoundException The input sequence contains more than one matching element.
380+ */
381+ public T singleOrDefault (Predicate <T > predicate , T defaultValue );
279382
280383 /**
281384 * Creates a List from a Stream.
0 commit comments