forked from WegraLee/effective-java-3e-source-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMax.java
More file actions
49 lines (42 loc) · 1.67 KB
/
Copy pathMax.java
File metadata and controls
49 lines (42 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package effectivejava.chapter8.item55;
import java.util.*;
// 반환 타입으로 Optional<T> 사용하기 (327-328쪽)
public class Max {
// // 코드 55-1 컬렉션에서 최댓값을 구한다. - 컬렉션이 비었으면 예외를 던진다. (327쪽)
// public static <E extends Comparable<E>> E max(Collection<E> c) {
// if (c.isEmpty())
// throw new IllegalArgumentException("빈 컬렉션");
//
// E result = null;
// for (E e : c)
// if (result == null || e.compareTo(result) > 0)
// result = Objects.requireNonNull(e);
//
// return result;
// }
// // 코드 55-2 컬렉션에서 최댓값을 구해 Optional<E>로 반환한다. (327쪽)
// public static <E extends Comparable<E>>
// Optional<E> max(Collection<E> c) {
// if (c.isEmpty())
// return Optional.empty();
//
// E result = null;
// for (E e : c)
// if (result == null || e.compareTo(result) > 0)
// result = Objects.requireNonNull(e);
//
// return Optional.of(result);
// }
// 코드 55-3 컬렉션에서 최댓값을 구해 Optional<E>로 반환한다. - 스트림 버전 (328쪽)
public static <E extends Comparable<E>>
Optional<E> max(Collection<E> c) {
return c.stream().max(Comparator.naturalOrder());
}
public static void main(String[] args) {
List<String> words = Arrays.asList(args);
System.out.println(max(words));
// 코드 55-4 옵셔널 활용 1 - 기본값을 정해둘 수 있다. (328쪽)
String lastWordInLexicon = max(words).orElse("단어 없음...");
System.out.println(lastWordInLexicon);
}
}