厨二的に。
とりあえず前半だけ
java
1package teratail;
2
3import java.util.Arrays;
4import java.util.Formatter;
5import java.util.stream.Collectors;
6
7public class Teratail40137 {
8 public static void main(String[] args) {
9 System.out.println(Arrays.stream(new String[][] {{"鳥谷","239","80"},{"大島","267","90"},{"糸井","231","82"},{"柳田","217","73"}}).map(x -> new Formatter().format("%sは、今季 6 月 15 日現在 %s 打数 %s安打で,打率 %fです.\n", x[0], x[1], x[2], Double.parseDouble(x[2])/Double.parseDouble(x[1])).toString()).collect(Collectors.joining()));
10 }
11}
んー。なんかきれいじゃないな
後半も作ってみた:
java
1package teratail;
2
3import java.util.Arrays;
4import java.util.Comparator;
5import java.util.Formatter;
6import java.util.stream.Collectors;
7
8public class Teratail40137 {
9 public static void main(String[] args) {
10 Player[] players = { new Player("鳥谷", 239, 80), new Player("大島", 267, 90), new Player("糸井", 231, 82), new Player("柳田", 217, 73) };
11 System.out.println(Arrays.stream(players).map(Player::toString).collect(Collectors.joining()));
12 Arrays.stream(players).max(Comparator.comparing(x -> x.ratio())).ifPresent(x -> {System.out.println("打率が最も高いのは: " + x.name);});
13 }
14}
15
16class Player {
17 final String name;
18 final int sheet;
19 final int hit;
20
21 public Player(String name, int sheet, int hit) {
22 this.name = name;
23 this.sheet = sheet;
24 this.hit = hit;
25 }
26
27 double ratio() {
28 return (double) hit / (double) sheet;
29 }
30
31 public String toString() {
32 try (Formatter f = new Formatter()) {
33 return f.format("%sは、今季 6 月 15 日現在 %s 打数 %s安打で,打率 %fです.\n", name, sheet, hit, ratio()).toString();
34 }
35 }
36}
やっぱりきれいじゃない。クラス作らなきゃいけないもんかなあ。
SimpleEntry 使ってみる:
java
1package teratail;
2
3import java.util.AbstractMap.SimpleEntry;
4import java.util.Arrays;
5import java.util.Comparator;
6
7public class Teratail40137 {
8
9 static SimpleEntry<SimpleEntry, Double> reportAndRatio(SimpleEntry e) {
10 int[] v = (int[]) e.getValue();
11 double r = (double) v[1] / (double) v[0];
12 System.out.printf("%sは、今季 6 月 15 日現在 %s打数 %s安打で,打率 %fです.\n", e.getKey(), v[0], v[1], r);
13 return new SimpleEntry(e, r);
14 }
15
16 public static void main(String[] args) {
17 SimpleEntry[] players = {
18 new SimpleEntry("鳥谷", new int[]{239, 80}),
19 new SimpleEntry("大島", new int[]{267, 90}),
20 new SimpleEntry("糸井", new int[]{231, 82}),
21 new SimpleEntry("柳田", new int[]{217, 73}) };
22
23 Arrays.stream(players)
24 .map(Teratail40137::reportAndRatio)
25 .max(Comparator.comparing(x -> x.getValue()))
26 .ifPresent(x -> {System.out.println("打率が最も高いのは: " + x.getKey().getKey());});
27 }
28}
こんなもんかな。いろいろ勉強になった。