回答編集履歴

3

typo

2020/04/25 00:16

投稿

shiketa
shiketa

スコア3971

test CHANGED
@@ -122,7 +122,7 @@
122
122
 
123
123
  .groupBy { it.category }
124
124
 
125
- .map { entry -> entry.key to entry.value.sortedBy { x -> x.name } }
125
+ .map { entry -> entry.key to entry.value.map { it.name }.sorted() }
126
126
 
127
127
  .toMap(EnumMap(Musician.Category::class.java))
128
128
 

2

内容修正

2020/04/25 00:16

投稿

shiketa
shiketa

スコア3971

test CHANGED
@@ -24,8 +24,110 @@
24
24
 
25
25
  Musician::getCategory,
26
26
 
27
- EnumMap::new,
27
+ // EnumMap::new, <- こうは、書けないので訂正します
28
+
29
+ () -> new EnumMap<>(Musician.Category.class),
28
30
 
29
31
  Collectors.mapping(Musician::getName, Collectors.toList())));
30
32
 
31
33
  ```
34
+
35
+
36
+
37
+ ---
38
+
39
+
40
+
41
+ xebmeさんからのお題。
42
+
43
+
44
+
45
+ > 2020/04/25 00:47
46
+
47
+ > グループ分けされた音楽家名もCollectors#toCollectionで並べかえてください
48
+
49
+
50
+
51
+ わたしには、荷が重すぎました。Collectors#toCollection()の例。是非ご提示いただければ。 > xebmeさん
52
+
53
+
54
+
55
+ ```java
56
+
57
+ for (int count = 0; count < 3; count++) {
58
+
59
+ Collections.shuffle(musicList);
60
+
61
+ final Map<Musician.Category, List<String>> map = musicList.stream().collect(Collectors
62
+
63
+ .groupingBy(Musician::getCategory,
64
+
65
+ () -> new EnumMap<>(Musician.Category.class),
66
+
67
+ Collectors.mapping(Musician::getName, Collectors.toCollection(() -> new ArrayList<>()))))
68
+
69
+ .entrySet().stream()
70
+
71
+ .collect(
72
+
73
+ Collectors.toMap(
74
+
75
+ x -> x.getKey(),
76
+
77
+ k -> {
78
+
79
+ Collections.sort(k.getValue());
80
+
81
+ return k.getValue();
82
+
83
+ },
84
+
85
+ (a, b) -> {
86
+
87
+ System.out.println(" a: " + a);
88
+
89
+ System.out.println(" b: " + b);
90
+
91
+ a.addAll(b);
92
+
93
+ Collections.sort(a);
94
+
95
+ return a;
96
+
97
+ },
98
+
99
+ () -> new EnumMap<>(Musician.Category.class)));
100
+
101
+
102
+
103
+ System.out.println(String.format("(%s) (%s) : %s", count, map.getClass().getSimpleName(), map));
104
+
105
+ }
106
+
107
+ ```
108
+
109
+
110
+
111
+ こっちだと、みじかく書けるのですが (^^;
112
+
113
+
114
+
115
+ ```kotlin
116
+
117
+ repeat(3) { count ->
118
+
119
+ val map =
120
+
121
+ musicList.shuffled()
122
+
123
+ .groupBy { it.category }
124
+
125
+ .map { entry -> entry.key to entry.value.sortedBy { x -> x.name } }
126
+
127
+ .toMap(EnumMap(Musician.Category::class.java))
128
+
129
+ println("(%s) (%s) : %s".format(count, map.javaClass.simpleName, map))
130
+
131
+ }
132
+
133
+ ```

1

内容修正

2020/04/24 22:39

投稿

shiketa
shiketa

スコア3971

test CHANGED
@@ -1,4 +1,18 @@
1
- Mapのキーの並びが気に入らないのであれば、キーでソートしてくれるインスタンスを渡せばよろしいのではないかと。
1
+ ~~Mapのキーの並びが気に入らないのであれば、キーでソートしてくれるインスタンスを渡せばよろしいのではないかと。~~
2
+
3
+
4
+
5
+ swordoneさんの指摘を反映しました。
6
+
7
+
8
+
9
+ Mapのキーの並びが気に入らないのであれば、キーの自然順序(enum定数の宣言される順序)に並べてくれるインスタンスを渡せばよろしいのではないかと。
10
+
11
+
12
+
13
+ * [EnumMap](https://docs.oracle.com/javase/jp/8/docs/api/java/util/EnumMap.html)
14
+
15
+
2
16
 
3
17
  ```java
4
18
 
@@ -10,7 +24,7 @@
10
24
 
11
25
  Musician::getCategory,
12
26
 
13
- TreeMap::new,
27
+ EnumMap::new,
14
28
 
15
29
  Collectors.mapping(Musician::getName, Collectors.toList())));
16
30