質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Q&A

解決済

2回答

1690閲覧

Java8のstreamで、自作オブジェクトのListの並べ替えをしたい

omodai

総合スコア10

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

0グッド

0クリップ

投稿2017/07/13 09:57

編集2017/07/13 10:49

###前提・実現したいこと
Java8のStreamを使用して自作オブジェクトのListの並べ替えをしたいと考えています
テストとして、PersonクラスとSectionクラス、その二つを持っているInfoクラスを作りました

  • Personクラス

java

1package sorttest; 2 3public class Person { 4 5 String firstName; 6 String lastName; 7 int age; 8 9 public Person(String firstName, String lastName, int age) { 10 this.firstName = firstName; 11 this.lastName = lastName; 12 this.age = age; 13 } 14 15 public String getFirstName() { 16 return firstName; 17 } 18 19 public String getLastName() { 20 return lastName; 21 } 22 23 public int getAge() { 24 return age; 25 } 26 27 @Override 28 public String toString() { 29 return "Person{" + "firstName=" + firstName + ", lastName=" + lastName + ", age=" + age + '}'; 30 } 31}
  • Sectionクラス

java

1package sorttest; 2 3public class Section { 4 5 String sectionCode; 6 String sectionName; 7 8 public Section(String sectionCode, String sectionName) { 9 this.sectionCode = sectionCode; 10 this.sectionName = sectionName; 11 } 12 13 public String getSectionCode() { 14 return sectionCode; 15 } 16 17 public String getSectionName() { 18 return sectionName; 19 } 20 21 @Override 22 public String toString() { 23 return "Section{" + "sectionCode=" + sectionCode + ", sectionName=" + sectionName + '}'; 24 } 25}
  • Infoクラス

java

1package sorttest; 2 3public class Info { 4 5 Person person; 6 Section section; 7 8 public Info(Person person, Section section) { 9 this.person = person; 10 this.section = section; 11 } 12 13 public Person getPerson() { 14 return person; 15 } 16 17 public Section getSection() { 18 return section; 19 } 20 21 @Override 22 public String toString() { 23 return "Info{" + "person=" + person.toString() + ", section=" + section.toString() + '}'; 24 } 25}

###実行したソースコード

以下の様に単純な形のリストだとStreamでcomparingを使用して並び替えができます

java

1package sorttest; 2 3import java.util.ArrayList; 4import java.util.Comparator; 5import java.util.List; 6import java.util.stream.Collectors; 7 8public class SortTest { 9 10 public static void main(String[] args) { 11 12 List<Section> sectionList = new ArrayList<>(); 13 14 sectionList.add(new Section("A", "あああ")); 15 sectionList.add(new Section("C", "ううう")); 16 sectionList.add(new Section("D", "えええ")); 17 sectionList.add(new Section("B", "ううう")); 18 19 20 System.out.println("sectionListをループ"); 21 for (Section section : sectionList) { 22 System.out.println(section.toString()); 23 } 24 25 System.out.println("-------------------------------------------------------"); 26 System.out.println("-------------------------------------------------------"); 27 System.out.println("-------------------------------------------------------"); 28 29 System.out.println("sectionListをsectionNameでソートしてからループ"); 30 31 List<Section> sectionList2 = new ArrayList<>(); 32 sectionList2.addAll(sectionList.stream().sorted( 33 Comparator.comparing(Section::getSectionCode)) 34 .collect(Collectors.toList())); 35 for (Section section : sectionList2) { 36 System.out.println(section.toString()); 37 } 38}
  • 実行結果
sectionListをループ Section{sectionCode=A, sectionName=あああ} Section{sectionCode=C, sectionName=ううう} Section{sectionCode=D, sectionName=えええ} Section{sectionCode=B, sectionName=ううう} ------------------------------------------------------- ------------------------------------------------------- ------------------------------------------------------- sectionListをsectionNameでソートしてからループ Section{sectionCode=A, sectionName=あああ} Section{sectionCode=B, sectionName=ううう} Section{sectionCode=C, sectionName=ううう} Section{sectionCode=D, sectionName=えええ}

###はまっているところ

以下のようにInfoオブジェクトの中にあるSectionオブジェクトの複数フィールドでソートしたい場合はどうするのがいいのでしょうか?

java

1package sorttest; 2 3import java.util.ArrayList; 4import java.util.Comparator; 5import java.util.List; 6import java.util.stream.Collectors; 7 8public class SortTest { 9 10 public static void main(String[] args) { 11 System.out.println("infoListをループ"); 12 List<Info> infoList = new ArrayList<>(); 13 infoList.add(new Info(new Person("taro", "yamada", 30), new Section("A", "あああ"))); 14 infoList.add(new Info(new Person("sachiko", "sato", 28), new Section("C", "ううう"))); 15 infoList.add(new Info(new Person("ichiro", "suzuki", 40), new Section("D", "えええ"))); 16 infoList.add(new Info(new Person("Eri", "ito", 20), new Section("B", "ううう"))); 17 18 for (Info info : infoList) { 19 System.out.println(info.getPerson().toString()); 20 System.out.println(info.getSection().toString()); 21 System.out.println("-------------------------------------------------------"); 22 } 23 } 24}
  • 実行結果
infoListをループ Person{firstName=taro, lastName=yamada, age=30} Section{sectionCode=A, sectionName=あああ} ------------------------------------------------------- Person{firstName=sachiko, lastName=sato, age=28} Section{sectionCode=C, sectionName=ううう} ------------------------------------------------------- Person{firstName=ichiro, lastName=suzuki, age=40} Section{sectionCode=D, sectionName=えええ} ------------------------------------------------------- Person{firstName=Eri, lastName=ito, age=20} Section{sectionCode=B, sectionName=ううう} -------------------------------------------------------
  • SectionのsectionNameでソートして以下のようにしたい
  • **追記・・・**sectionNameだけでなくsectionCodeと合わせてソートするにはどうすればいいでしょうか?
Person{firstName=taro, lastName=yamada, age=30} Section{sectionCode=A, sectionName=あああ} ------------------------------------------------------- Person{firstName=Eri, lastName=ito, age=20} Section{sectionCode=B, sectionName=ううう} ------------------------------------------------------- Person{firstName=sachiko, lastName=sato, age=28} Section{sectionCode=C, sectionName=ううう} ------------------------------------------------------- Person{firstName=ichiro, lastName=suzuki, age=40} Section{sectionCode=D, sectionName=えええ} -------------------------------------------------------
  • よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

Infoクラスにimplements Comparableすればいいのでは?

java

1public class Info implements Comparable<Info> { 2 3 public int compareTo(Info info) { 4 int i = this.section.getSectionName().compareTo(info.section.getSectionName()); 5 return i == 0 ? this.section.getSectionCode().compareTo(info.section.getSectionCode()) : i; 6 } 7 8}

こうすればソートはこうなります。

java

1List<Info> sortedList = infoList.stream().sorted().collect(Collectors.toList());

投稿2017/07/13 16:02

swordone

総合スコア20651

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

Java

1List<Info> sortedList = infoList.stream() 2 .sorted(Comparator.comparing(info -> info.getSection().getSectionName())) 3 .collect(Collectors.toList());

###追記

Java

1Comparator<Info> comparator = Comparator.<Info, String> comparing(info -> info.getSection().getSectionName()) 2 .thenComparing(info -> info.getSection().getSectionCode()); 3 4List<Info> sortedList = infoList.stream() 5 .sorted(comparator) 6 .collect(Collectors.toList());

投稿2017/07/13 10:16

編集2017/07/13 11:22
root_jp

総合スコア4666

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

omodai

2017/07/13 10:46

早速の回答ありがとうございます。 上記の記述でうまくいきました!! 質問の抜けがありました。 sectionName,sectionCodeの複合でソートしたい場合はどうすればいいかご存知でしょうか?
root_jp

2017/07/13 11:23

追記しました。 いい加減長いんで、Comparatorの部分を独立させましたが、ワンライナーで書きたければどうぞ。
omodai

2017/07/13 23:59

やりたいことができました。ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問