回答編集履歴
2
java.util.stream.Streamに訂正
answer
CHANGED
@@ -15,7 +15,7 @@
|
|
15
15
|
```
|
16
16
|
`""`の代わりに奪三振率の高い`"Yu Darvish"` としたいですね。(文字列リテラルなら何でもよい)
|
17
17
|
|
18
|
-
**java.util.Streamを使用する統計処理(追記)**
|
18
|
+
**java.util.stream.Streamを使用する統計処理(追記)**
|
19
19
|
|
20
20
|
バッターの打率を集計する専用クラス。Streamを使用して作り直したソースを追記します。
|
21
21
|
```Java
|
1
java.util.Streamを使用する例を追記
answer
CHANGED
@@ -13,4 +13,59 @@
|
|
13
13
|
matrix.put(row, "", value); // <---- (3)
|
14
14
|
}
|
15
15
|
```
|
16
|
-
`""`の代わりに奪三振率の高い`"Yu Darvish"` としたいですね。
|
16
|
+
`""`の代わりに奪三振率の高い`"Yu Darvish"` としたいですね。(文字列リテラルなら何でもよい)
|
17
|
+
|
18
|
+
**java.util.Streamを使用する統計処理(追記)**
|
19
|
+
|
20
|
+
バッターの打率を集計する専用クラス。Streamを使用して作り直したソースを追記します。
|
21
|
+
```Java
|
22
|
+
import java.io.BufferedReader;
|
23
|
+
import java.io.FileReader;
|
24
|
+
import java.io.IOException;
|
25
|
+
import java.io.PrintStream;
|
26
|
+
import java.util.Map;
|
27
|
+
import java.util.TreeMap;
|
28
|
+
import java.util.AbstractMap.SimpleEntry;
|
29
|
+
import java.util.stream.Collectors;
|
30
|
+
import java.util.stream.Stream;
|
31
|
+
|
32
|
+
public class EvaConv2 {
|
33
|
+
|
34
|
+
public static void main(String[] args) {
|
35
|
+
try {
|
36
|
+
PlayerMatrix.read("ファイル名.EVA").writeTo(System.out);
|
37
|
+
} catch (IOException e) {
|
38
|
+
e.printStackTrace();
|
39
|
+
}
|
40
|
+
}
|
41
|
+
|
42
|
+
private static class PlayerMatrix {
|
43
|
+
private final Map<String, Double> freqs;
|
44
|
+
|
45
|
+
public PlayerMatrix(Stream<String> lines) throws IOException {
|
46
|
+
freqs = lines.map(
|
47
|
+
line -> {String[] t = line.split(","); return new SimpleEntry<>(t[3], t[16]);
|
48
|
+
}).collect(
|
49
|
+
Collectors.groupingBy(
|
50
|
+
SimpleEntry::getKey,
|
51
|
+
TreeMap::new,
|
52
|
+
Collectors.averagingDouble(e -> Double.valueOf(e.getValue()))
|
53
|
+
));
|
54
|
+
}
|
55
|
+
|
56
|
+
void writeTo(PrintStream out) {
|
57
|
+
out.println("player,batting average (BA)");
|
58
|
+
freqs.entrySet().forEach(e -> out.printf("%s,%1.3f\n", e.getKey(), e.getValue()));
|
59
|
+
}
|
60
|
+
|
61
|
+
static PlayerMatrix read(String filename) throws IOException {
|
62
|
+
PlayerMatrix matrix = null;
|
63
|
+
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
|
64
|
+
matrix = new PlayerMatrix(br.lines());
|
65
|
+
}
|
66
|
+
return matrix;
|
67
|
+
}
|
68
|
+
}
|
69
|
+
}
|
70
|
+
```
|
71
|
+
たぶんこれより`R`で作る方が簡単でしょう。
|