diff --git a/spring-core/src/main/java/org/springframework/util/CollectionUtils.java b/spring-core/src/main/java/org/springframework/util/CollectionUtils.java index 79f5185796e1..84b1fd1c67ff 100644 --- a/spring-core/src/main/java/org/springframework/util/CollectionUtils.java +++ b/spring-core/src/main/java/org/springframework/util/CollectionUtils.java @@ -314,84 +314,86 @@ else if (candidate != val.getClass()) { } /** - * Retrieve the last element of the given Set, using {@link SortedSet#last()} - * or otherwise iterating over all elements (assuming a linked set). + * Retrieve the first element of the given Set, using {@link SortedSet#first()} + * or otherwise using the iterator. * @param set the Set to check (may be {@code null} or empty) - * @return the last element, or {@code null} if none - * @since 5.0.3 + * @return the first element, or {@code null} if none + * @since 5.2.3 * @see SortedSet * @see LinkedHashMap#keySet() * @see java.util.LinkedHashSet */ @Nullable - public static T lastElement(@Nullable Set set) { + public static T firstElement(@Nullable Set set) { if (isEmpty(set)) { return null; } if (set instanceof SortedSet) { - return ((SortedSet) set).last(); + return ((SortedSet) set).first(); } - // Full iteration necessary... Iterator it = set.iterator(); - T last = null; - while (it.hasNext()) { - last = it.next(); + T first = null; + if (it.hasNext()) { + first = it.next(); } - return last; + return first; } /** - * Retrieve the last element of the given List, accessing the highest index. + * Retrieve the first element of the given List, accessing the zero index. * @param list the List to check (may be {@code null} or empty) - * @return the last element, or {@code null} if none - * @since 5.0.3 + * @return the first element, or {@code null} if none + * @since 5.2.3 */ @Nullable - public static T lastElement(@Nullable List list) { + public static T firstElement(@Nullable List list) { if (isEmpty(list)) { return null; } - return list.get(list.size() - 1); + return list.get(0); } /** - * Retrieve the first element of the given Set, using {@link SortedSet#first()} - * or otherwise using the iterator. + * Retrieve the last element of the given Set, using {@link SortedSet#last()} + * or otherwise iterating over all elements (assuming a linked set). * @param set the Set to check (may be {@code null} or empty) - * @return the first element, or {@code null} if none + * @return the last element, or {@code null} if none + * @since 5.0.3 * @see SortedSet * @see LinkedHashMap#keySet() * @see java.util.LinkedHashSet */ @Nullable - public static T firstElement(@Nullable Set set) { + public static T lastElement(@Nullable Set set) { if (isEmpty(set)) { return null; } if (set instanceof SortedSet) { - return ((SortedSet) set).first(); + return ((SortedSet) set).last(); } + // Full iteration necessary... Iterator it = set.iterator(); - T first = null; - if (it.hasNext()) { - first = it.next(); + T last = null; + while (it.hasNext()) { + last = it.next(); } - return first; + return last; } /** - * Retrieve the first element of the given List, accessing the zero index. + * Retrieve the last element of the given List, accessing the highest index. * @param list the List to check (may be {@code null} or empty) - * @return the first element, or {@code null} if none + * @return the last element, or {@code null} if none + * @since 5.0.3 */ @Nullable - public static T firstElement(@Nullable List list) { + public static T lastElement(@Nullable List list) { if (isEmpty(list)) { return null; } - return list.get(0); + return list.get(list.size() - 1); } /**