いつもお世話になっております。
CollectorsクラスのgroupingByメソッドの仕様について、質問をさせていただきたいです。
上記のメソッドを使用した際に、グルーピングが行われますが、その結果を出力した際の出力順序には、決まりはないのでしょうか。
groupByの対象となるListの中身
- new Musician("Jimi Hendrix", Musician.Category.ROCK)
- new Musician("Eric Dolphy", Musician.Category.JAZZ)
- new Musician("J.S.Bach", Musician.Category.CLASSICAL)
- new Musician("Charles Mingus", Musician.Category.JAZZ)
Collectorsクラス:groupingBy Javadoc
リストの中身の順番を変えた場合の出力結果をいくつか記載致します。
<出力結果>
{JAZZ=[Eric Dolphy, Charles Mingus], ROCK=[Jimi Hendrix], CLASSICAL=[J.S.Bach]}
Java
1import java.util.Arrays; 2import java.util.List; 3import java.util.Map; 4import java.util.stream.Collectors; 5 6public class Test { 7 8 public static void main(String[] args) { 9 10 List<Musician> musicList = Arrays.asList( 11 new Musician("Jimi Hendrix", Musician.Category.ROCK), 12 new Musician("Eric Dolphy", Musician.Category.JAZZ), 13 new Musician("J.S.Bach", Musician.Category.CLASSICAL), 14 new Musician("Charles Mingus", Musician.Category.JAZZ)); 15 16 17 Map<Musician.Category, List<String>> map = musicList.stream().collect(Collectors 18 .groupingBy(Musician::getCategory, Collectors.mapping(Musician::getName, Collectors.toList()))); 19 20 System.out.println(map); 21 } 22 23} 24 25
Java
1public class Musician { 2 public enum Category { 3 ROCK, JAZZ, CLASSICAL 4 } 5 6 private String name; 7 private Category category; 8 9 public Musician(String name, Category category) { 10 this.name = name; 11 this.category = category; 12 } 13 14 public String getName() { 15 return name; 16 } 17 18 public void setName(String name) { 19 this.name = name; 20 } 21 22 public Category getCategory() { 23 return category; 24 } 25 26 public void setCategory(Category category) { 27 this.category = category; 28 } 29 30}
<出力結果>
{CLASSICAL=[J.S.Bach], JAZZ=[Eric Dolphy, Charles Mingus], ROCK=[Jimi Hendrix]}
Java
1import java.util.Arrays; 2import java.util.List; 3import java.util.Map; 4import java.util.stream.Collectors; 5 6public class Test { 7 8 public static void main(String[] args) { 9 10 List<Musician> musicList = Arrays.asList( 11 new Musician("Eric Dolphy", Musician.Category.JAZZ), 12 new Musician("J.S.Bach", Musician.Category.CLASSICAL), 13 new Musician("Charles Mingus", Musician.Category.JAZZ), 14 new Musician("Jimi Hendrix", Musician.Category.ROCK)); 15 16 Map<Musician.Category, List<String>> map = musicList.stream().collect(Collectors 17 .groupingBy(Musician::getCategory, Collectors.mapping(Musician::getName, Collectors.toList()))); 18 19 System.out.println(map); 20 } 21 22}
<出力結果>
{JAZZ=[Charles Mingus, Eric Dolphy], CLASSICAL=[J.S.Bach], ROCK=[Jimi Hendrix]}
Java
1import java.util.Arrays; 2import java.util.List; 3import java.util.Map; 4import java.util.stream.Collectors; 5 6public class Test { 7 8 public static void main(String[] args) { 9 10 List<Musician> musicList = Arrays.asList( 11 new Musician("J.S.Bach", Musician.Category.CLASSICAL), 12 new Musician("Charles Mingus", Musician.Category.JAZZ), 13 new Musician("Jimi Hendrix", Musician.Category.ROCK), 14 new Musician("Eric Dolphy", Musician.Category.JAZZ)); 15 16 Map<Musician.Category, List<String>> map = musicList.stream().collect(Collectors 17 .groupingBy(Musician::getCategory, Collectors.mapping(Musician::getName, Collectors.toList()))); 18 19 System.out.println(map); 20 } 21 22} 23
回答4件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/04/24 07:44
2020/04/24 12:26
2020/04/24 15:47
2020/04/24 22:40