質問するログイン新規登録

回答編集履歴

3

補足

2016/07/05 23:15

投稿

matobaa
matobaa

スコア2493

answer CHANGED
@@ -1,3 +1,5 @@
1
+ 厨二的に。
2
+
1
3
  とりあえず前半だけ
2
4
 
3
5
  ```java

2

SimpleEntryで作ってみた

2016/07/05 23:15

投稿

matobaa
matobaa

スコア2493

answer CHANGED
@@ -57,4 +57,37 @@
57
57
  }
58
58
  ```
59
59
  やっぱりきれいじゃない。クラス作らなきゃいけないもんかなあ。
60
+ SimpleEntry 使ってみる:
61
+
62
+ ```java
63
+ package teratail;
64
+
65
+ import java.util.AbstractMap.SimpleEntry;
66
+ import java.util.Arrays;
67
+ import java.util.Comparator;
68
+
69
+ public class Teratail40137 {
70
+
71
+ static SimpleEntry<SimpleEntry, Double> reportAndRatio(SimpleEntry e) {
72
+ int[] v = (int[]) e.getValue();
73
+ double r = (double) v[1] / (double) v[0];
74
+ System.out.printf("%sは、今季 6 月 15 日現在 %s打数 %s安打で,打率 %fです.\n", e.getKey(), v[0], v[1], r);
75
+ return new SimpleEntry(e, r);
76
+ }
77
+
78
+ public static void main(String[] args) {
79
+ SimpleEntry[] players = {
80
+ new SimpleEntry("鳥谷", new int[]{239, 80}),
81
+ new SimpleEntry("大島", new int[]{267, 90}),
82
+ new SimpleEntry("糸井", new int[]{231, 82}),
83
+ new SimpleEntry("柳田", new int[]{217, 73}) };
84
+
85
+ Arrays.stream(players)
86
+ .map(Teratail40137::reportAndRatio)
87
+ .max(Comparator.comparing(x -> x.getValue()))
88
+ .ifPresent(x -> {System.out.println("打率が最も高いのは: " + x.getKey().getKey());});
89
+ }
90
+ }
91
+ ```
92
+
60
- いろいろ勉強になった。
93
+ こんなもんかな。いろいろ勉強になった。

1

後半も作った

2016/07/05 23:13

投稿

matobaa
matobaa

スコア2493

answer CHANGED
@@ -14,4 +14,47 @@
14
14
  }
15
15
  ```
16
16
 
17
- んー。なんかきれいじゃないな
17
+ んー。なんかきれいじゃないな
18
+
19
+ 後半も作ってみた:
20
+
21
+ ```java
22
+ package teratail;
23
+
24
+ import java.util.Arrays;
25
+ import java.util.Comparator;
26
+ import java.util.Formatter;
27
+ import java.util.stream.Collectors;
28
+
29
+ public class Teratail40137 {
30
+ public static void main(String[] args) {
31
+ Player[] players = { new Player("鳥谷", 239, 80), new Player("大島", 267, 90), new Player("糸井", 231, 82), new Player("柳田", 217, 73) };
32
+ System.out.println(Arrays.stream(players).map(Player::toString).collect(Collectors.joining()));
33
+ Arrays.stream(players).max(Comparator.comparing(x -> x.ratio())).ifPresent(x -> {System.out.println("打率が最も高いのは: " + x.name);});
34
+ }
35
+ }
36
+
37
+ class Player {
38
+ final String name;
39
+ final int sheet;
40
+ final int hit;
41
+
42
+ public Player(String name, int sheet, int hit) {
43
+ this.name = name;
44
+ this.sheet = sheet;
45
+ this.hit = hit;
46
+ }
47
+
48
+ double ratio() {
49
+ return (double) hit / (double) sheet;
50
+ }
51
+
52
+ public String toString() {
53
+ try (Formatter f = new Formatter()) {
54
+ return f.format("%sは、今季 6 月 15 日現在 %s 打数 %s安打で,打率 %fです.\n", name, sheet, hit, ratio()).toString();
55
+ }
56
+ }
57
+ }
58
+ ```
59
+ やっぱりきれいじゃない。クラス作らなきゃいけないもんかなあ。
60
+ いろいろ勉強になった。