Update 30. 优先使用泛型方法.md

This commit is contained in:
Joe 2019-11-26 15:59:18 +08:00 committed by GitHub
parent e553edf61a
commit 3a5a953b7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -114,18 +114,13 @@ public static <E extends Comparable<E>> E max(Collection<E> c);
```java ```java
// Returns max value in a collection - uses recursive type bound // Returns max value in a collection - uses recursive type bound
public static <E extends Comparable<E>> E max(Collection<E> c) { public static <E extends Comparable<E>> E max(Collection<E> c) {
if (c.isEmpty())
if (c.isEmpty()) throw new IllegalArgumentException("Empty collection");
throw new IllegalArgumentException("Empty collection"); E result = null;
for (E e : c)
E result = null; if (result == null || e.compareTo(result) > 0)
result = Objects.requireNonNull(e);
for (E e : c) return result;
if (result == null || [e.compareTo(result](http://e.compareTo(result)) > 0)
result = Objects.requireNonNull(e);
return result;
} }
``` ```