@@ -314,84 +314,86 @@ else if (candidate != val.getClass()) {
314
314
}
315
315
316
316
/**
317
- * Retrieve the last element of the given Set, using {@link SortedSet#last ()}
318
- * or otherwise iterating over all elements (assuming a linked set) .
317
+ * Retrieve the first element of the given Set, using {@link SortedSet#first ()}
318
+ * or otherwise using the iterator .
319
319
* @param set the Set to check (may be {@code null} or empty)
320
- * @return the last element, or {@code null} if none
321
- * @since 5.0 .3
320
+ * @return the first element, or {@code null} if none
321
+ * @since 5.2 .3
322
322
* @see SortedSet
323
323
* @see LinkedHashMap#keySet()
324
324
* @see java.util.LinkedHashSet
325
325
*/
326
326
@ Nullable
327
- public static <T > T lastElement (@ Nullable Set <T > set ) {
327
+ public static <T > T firstElement (@ Nullable Set <T > set ) {
328
328
if (isEmpty (set )) {
329
329
return null ;
330
330
}
331
331
if (set instanceof SortedSet ) {
332
- return ((SortedSet <T >) set ).last ();
332
+ return ((SortedSet <T >) set ).first ();
333
333
}
334
334
335
- // Full iteration necessary...
336
335
Iterator <T > it = set .iterator ();
337
- T last = null ;
338
- while (it .hasNext ()) {
339
- last = it .next ();
336
+ T first = null ;
337
+ if (it .hasNext ()) {
338
+ first = it .next ();
340
339
}
341
- return last ;
340
+ return first ;
342
341
}
343
342
344
343
/**
345
- * Retrieve the last element of the given List, accessing the highest index.
344
+ * Retrieve the first element of the given List, accessing the zero index.
346
345
* @param list the List to check (may be {@code null} or empty)
347
- * @return the last element, or {@code null} if none
348
- * @since 5.0 .3
346
+ * @return the first element, or {@code null} if none
347
+ * @since 5.2 .3
349
348
*/
350
349
@ Nullable
351
- public static <T > T lastElement (@ Nullable List <T > list ) {
350
+ public static <T > T firstElement (@ Nullable List <T > list ) {
352
351
if (isEmpty (list )) {
353
352
return null ;
354
353
}
355
- return list .get (list . size () - 1 );
354
+ return list .get (0 );
356
355
}
357
356
358
357
/**
359
- * Retrieve the first element of the given Set, using {@link SortedSet#first ()}
360
- * or otherwise using the iterator .
358
+ * Retrieve the last element of the given Set, using {@link SortedSet#last ()}
359
+ * or otherwise iterating over all elements (assuming a linked set) .
361
360
* @param set the Set to check (may be {@code null} or empty)
362
- * @return the first element, or {@code null} if none
361
+ * @return the last element, or {@code null} if none
362
+ * @since 5.0.3
363
363
* @see SortedSet
364
364
* @see LinkedHashMap#keySet()
365
365
* @see java.util.LinkedHashSet
366
366
*/
367
367
@ Nullable
368
- public static <T > T firstElement (@ Nullable Set <T > set ) {
368
+ public static <T > T lastElement (@ Nullable Set <T > set ) {
369
369
if (isEmpty (set )) {
370
370
return null ;
371
371
}
372
372
if (set instanceof SortedSet ) {
373
- return ((SortedSet <T >) set ).first ();
373
+ return ((SortedSet <T >) set ).last ();
374
374
}
375
375
376
+ // Full iteration necessary...
376
377
Iterator <T > it = set .iterator ();
377
- T first = null ;
378
- if (it .hasNext ()) {
379
- first = it .next ();
378
+ T last = null ;
379
+ while (it .hasNext ()) {
380
+ last = it .next ();
380
381
}
381
- return first ;
382
+ return last ;
382
383
}
383
384
384
385
/**
385
- * Retrieve the first element of the given List, accessing the zero index.
386
+ * Retrieve the last element of the given List, accessing the highest index.
386
387
* @param list the List to check (may be {@code null} or empty)
387
- * @return the first element, or {@code null} if none
388
+ * @return the last element, or {@code null} if none
389
+ * @since 5.0.3
388
390
*/
389
391
@ Nullable
390
- public static <T > T firstElement (@ Nullable List <T > list ) {
392
+ public static <T > T lastElement (@ Nullable List <T > list ) {
391
393
if (isEmpty (list )) {
392
394
return null ;
393
395
}
394
- return list .get (0 );
396
+ return list .get (list . size () - 1 );
395
397
}
396
398
397
399
/**
0 commit comments