Skip to content

Commit c2141e2

Browse files
committed
Add @SInCE tags to firstElement methods
1 parent d503bc2 commit c2141e2

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

spring-core/src/main/java/org/springframework/util/CollectionUtils.java

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -314,84 +314,86 @@ else if (candidate != val.getClass()) {
314314
}
315315

316316
/**
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.
319319
* @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
322322
* @see SortedSet
323323
* @see LinkedHashMap#keySet()
324324
* @see java.util.LinkedHashSet
325325
*/
326326
@Nullable
327-
public static <T> T lastElement(@Nullable Set<T> set) {
327+
public static <T> T firstElement(@Nullable Set<T> set) {
328328
if (isEmpty(set)) {
329329
return null;
330330
}
331331
if (set instanceof SortedSet) {
332-
return ((SortedSet<T>) set).last();
332+
return ((SortedSet<T>) set).first();
333333
}
334334

335-
// Full iteration necessary...
336335
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();
340339
}
341-
return last;
340+
return first;
342341
}
343342

344343
/**
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.
346345
* @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
349348
*/
350349
@Nullable
351-
public static <T> T lastElement(@Nullable List<T> list) {
350+
public static <T> T firstElement(@Nullable List<T> list) {
352351
if (isEmpty(list)) {
353352
return null;
354353
}
355-
return list.get(list.size() - 1);
354+
return list.get(0);
356355
}
357356

358357
/**
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).
361360
* @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
363363
* @see SortedSet
364364
* @see LinkedHashMap#keySet()
365365
* @see java.util.LinkedHashSet
366366
*/
367367
@Nullable
368-
public static <T> T firstElement(@Nullable Set<T> set) {
368+
public static <T> T lastElement(@Nullable Set<T> set) {
369369
if (isEmpty(set)) {
370370
return null;
371371
}
372372
if (set instanceof SortedSet) {
373-
return ((SortedSet<T>) set).first();
373+
return ((SortedSet<T>) set).last();
374374
}
375375

376+
// Full iteration necessary...
376377
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();
380381
}
381-
return first;
382+
return last;
382383
}
383384

384385
/**
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.
386387
* @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
388390
*/
389391
@Nullable
390-
public static <T> T firstElement(@Nullable List<T> list) {
392+
public static <T> T lastElement(@Nullable List<T> list) {
391393
if (isEmpty(list)) {
392394
return null;
393395
}
394-
return list.get(0);
396+
return list.get(list.size() - 1);
395397
}
396398

397399
/**

0 commit comments

Comments
 (0)