forked from jbloch/effective-java-3e-source-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionClassifier.java
More file actions
34 lines (29 loc) · 989 Bytes
/
Copy pathCollectionClassifier.java
File metadata and controls
34 lines (29 loc) · 989 Bytes
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
package effectivejava.chapter8.item52;
import java.util.*;
import java.math.*;
// Broken! - What does this program print? (Page 238)
public class CollectionClassifier {
public static String classify(Set<?> s) {
return "Set";
}
public static String classify(List<?> lst) {
return "List";
}
public static String classify(Collection<?> c) {
return "Unknown Collection";
}
public static void main(String[] args) {
Collection<?>[] collections = {
new HashSet<String>(),
new ArrayList<BigInteger>(),
new HashMap<String, String>().values()
};
for (Collection<?> c : collections)
System.out.println(classify(c));
}
// Repaired static classifier method. (Page 240)
// public static String classify(Collection<?> c) {
// return c instanceof Set ? "Set" :
// c instanceof List ? "List" : "Unknown Collection";
// }
}