回答編集履歴
3
typo
answer
CHANGED
@@ -60,7 +60,7 @@
|
|
60
60
|
val map =
|
61
61
|
musicList.shuffled()
|
62
62
|
.groupBy { it.category }
|
63
|
-
.map { entry -> entry.key to entry.value.
|
63
|
+
.map { entry -> entry.key to entry.value.map { it.name }.sorted() }
|
64
64
|
.toMap(EnumMap(Musician.Category::class.java))
|
65
65
|
println("(%s) (%s) : %s".format(count, map.javaClass.simpleName, map))
|
66
66
|
}
|
2
内容修正
answer
CHANGED
@@ -11,6 +11,57 @@
|
|
11
11
|
musicList.stream().collect(
|
12
12
|
Collectors.groupingBy(
|
13
13
|
Musician::getCategory,
|
14
|
-
EnumMap::new,
|
14
|
+
// EnumMap::new, <- こうは、書けないので訂正します
|
15
|
+
() -> new EnumMap<>(Musician.Category.class),
|
15
16
|
Collectors.mapping(Musician::getName, Collectors.toList())));
|
17
|
+
```
|
18
|
+
|
19
|
+
---
|
20
|
+
|
21
|
+
xebmeさんからのお題。
|
22
|
+
|
23
|
+
> 2020/04/25 00:47
|
24
|
+
> グループ分けされた音楽家名もCollectors#toCollectionで並べかえてください
|
25
|
+
|
26
|
+
わたしには、荷が重すぎました。Collectors#toCollection()の例。是非ご提示いただければ。 > xebmeさん
|
27
|
+
|
28
|
+
```java
|
29
|
+
for (int count = 0; count < 3; count++) {
|
30
|
+
Collections.shuffle(musicList);
|
31
|
+
final Map<Musician.Category, List<String>> map = musicList.stream().collect(Collectors
|
32
|
+
.groupingBy(Musician::getCategory,
|
33
|
+
() -> new EnumMap<>(Musician.Category.class),
|
34
|
+
Collectors.mapping(Musician::getName, Collectors.toCollection(() -> new ArrayList<>()))))
|
35
|
+
.entrySet().stream()
|
36
|
+
.collect(
|
37
|
+
Collectors.toMap(
|
38
|
+
x -> x.getKey(),
|
39
|
+
k -> {
|
40
|
+
Collections.sort(k.getValue());
|
41
|
+
return k.getValue();
|
42
|
+
},
|
43
|
+
(a, b) -> {
|
44
|
+
System.out.println(" a: " + a);
|
45
|
+
System.out.println(" b: " + b);
|
46
|
+
a.addAll(b);
|
47
|
+
Collections.sort(a);
|
48
|
+
return a;
|
49
|
+
},
|
50
|
+
() -> new EnumMap<>(Musician.Category.class)));
|
51
|
+
|
52
|
+
System.out.println(String.format("(%s) (%s) : %s", count, map.getClass().getSimpleName(), map));
|
53
|
+
}
|
54
|
+
```
|
55
|
+
|
56
|
+
こっちだと、みじかく書けるのですが (^^;
|
57
|
+
|
58
|
+
```kotlin
|
59
|
+
repeat(3) { count ->
|
60
|
+
val map =
|
61
|
+
musicList.shuffled()
|
62
|
+
.groupBy { it.category }
|
63
|
+
.map { entry -> entry.key to entry.value.sortedBy { x -> x.name } }
|
64
|
+
.toMap(EnumMap(Musician.Category::class.java))
|
65
|
+
println("(%s) (%s) : %s".format(count, map.javaClass.simpleName, map))
|
66
|
+
}
|
16
67
|
```
|
1
内容修正
answer
CHANGED
@@ -1,9 +1,16 @@
|
|
1
|
-
Mapのキーの並びが気に入らないのであれば、キーでソートしてくれるインスタンスを渡せばよろしいのではないかと。
|
1
|
+
~~Mapのキーの並びが気に入らないのであれば、キーでソートしてくれるインスタンスを渡せばよろしいのではないかと。~~
|
2
|
+
|
3
|
+
swordoneさんの指摘を反映しました。
|
4
|
+
|
5
|
+
Mapのキーの並びが気に入らないのであれば、キーの自然順序(enum定数の宣言される順序)に並べてくれるインスタンスを渡せばよろしいのではないかと。
|
6
|
+
|
7
|
+
* [EnumMap](https://docs.oracle.com/javase/jp/8/docs/api/java/util/EnumMap.html)
|
8
|
+
|
2
9
|
```java
|
3
10
|
Map<Musician.Category, List<String>> map =
|
4
11
|
musicList.stream().collect(
|
5
12
|
Collectors.groupingBy(
|
6
13
|
Musician::getCategory,
|
7
|
-
|
14
|
+
EnumMap::new,
|
8
15
|
Collectors.mapping(Musician::getName, Collectors.toList())));
|
9
16
|
```
|