実現したいこと
List<Staff> staffList1 = {("1", "nakamoto"), ("2", "takagi"), ("3", "katoh"), ("4", "shimura")}
List<Staff> staffList2 = {("1", "AAA"), ("2", "BBB"), }
class Staff{
String id;
String name;
}
上記2つのリストのうち、idが重複している項目を、Streamを使用して削除したいです。つまり、
List<Staff> staffList3 = {("3", "katoh"), ("4", "shimura")}
というリストを新たに作りたいです。
発生している問題・分からないこと
Stream.filter、flatMap(多重ループ)、Predicate.not(equalを否定)で、「idが同じでない場合」という条件でやってみたのですが、上記を実現できませんでした。
該当のソースコード
Jave
1List<Staff> staffList3 = 2 staffList1.stream() 3 .flatMap( 4 i -> staffList2.stream() 5 .filter(Predicate.not(j -> i.getId().equals(j.getId()))) 6 ) 7 .collect(Collectors.toList());
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
equalsの真逆をやりたいのですが、うまくできません。
補足
特になし
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2024/09/16 06:44

回答5件
0
「staffList1
の要素のうち、staffList2
に同じIDの要素が含まれていないものだけを取り出したい」という認識でよいでしょうか? これをStream
で実現する方法はいろいろあると思いますが、たとえば以下のように実装することが可能です。
java
1List<Staff> staffList3 = staffList1.stream().filter(s1 -> !staffList2.stream().anyMatch(s2 -> s1.getId().equals(s2.getId()))).collect(Collectors.toList());
あるいは「staffList1
とstaffList2
の要素のうち、片方のリストにのみ存在するものを抽出したい」ということであれば、次のような実装になるかと思います。
java
1List<Staff> staffList4 = Stream.concat( 2 staffList1.stream().filter(s1 -> !staffList2.stream().anyMatch(s2 -> s1.getId().equals(s2.getId()))), 3 staffList2.stream().filter(s2 -> !staffList1.stream().anyMatch(s1 -> s1.getId().equals(s2.getId()))) 4).collect(Collectors.toList());
投稿2024/09/13 16:07
総合スコア2386
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
0
上記2つのリストのうち、idが重複している項目を、Streamを使用して削除したいです。つまり、
List<Staff> staffList3 = {("3", "katoh"), ("4", "shimura")}
というリストを新たに作りたいです。
集合演算ですね。
差集合か対称差かが提示例からはわかりませんが、差集合でいいんかな?(対称差ならお互いの差集合を結合してください)
差集合 - Wikipedia
対称差 - Wikipedia
java
1import java.util.*; 2import java.util.stream.Collectors; 3 4public class Sample { 5 public static void main(String[] args) { 6 List<Staff> staffList1 = List.of( 7 new Staff("1", "nakamoto"), 8 new Staff("2", "takagi"), 9 new Staff("3", "katoh"), 10 new Staff("4", "shimura")); 11 12 List<Staff> staffList2 = List.of( 13 new Staff("1", "AAA"), 14 new Staff("2", "BBB"), 15 new Staff("26", "ZZZ")); 16 17 // staffList2になかったものだけにフィルタ 18 List<Staff> staffList3 = staffList1.stream() 19 .filter(i -> staffList2.stream().noneMatch(j -> i.id.equals(j.id))) 20 .collect(Collectors.toList()); 21 System.out.println(staffList3); 22 23 // staffList2にあったものを削除 24// staffList3 = new ArrayList<>(staffList1); 25// staffList3.removeIf(i -> staffList2.stream().anyMatch(j -> i.id.equals(j.id))); 26// System.out.println(staffList3); 27 28 // equalsをカスタムしていいならremoveAllだけの話だが 29// staffList3 = new ArrayList<>(staffList1); 30// staffList3.removeAll(staffList2); 31// System.out.println(staffList3); 32 } 33 34 // 面倒なのでrecord(別にclassでも同じ) 35 record Staff(String id, String name) { 36// @Override public boolean equals(Object o) { 37// if (this == o) return true; 38// if (o == null || getClass() != o.getClass()) return false; 39// Staff staff = (Staff) o; 40// return Objects.equals(id, staff.id); 41// } 42// @Override public int hashCode() { return Objects.hash(id); } 43 } 44}
Stream#noneMatch (Java SE 21 & JDK 21)
ArrayList#removeIf (Java SE 21 & JDK 21)
Stream#anyMatch (Java SE 21 & JDK 21)
ArrayList#removeAll (Java SE 21 & JDK 21)
[Staff[id=3, name=katoh], Staff[id=4, name=shimura]]
投稿2024/09/13 21:55
総合スコア10104
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。

0
ベストアンサー
staffList1 や 2 の定義等は普通に java ソースコードとして載せて頂けると助かります。
flatMap を使うならこんな風?
java
1import java.util.*; 2import java.util.stream.*; 3 4public class Main { 5 public static void main(String[] args) throws Exception { 6 List<Staff> staffList1 = List.of(new Staff("1", "nakamoto"), new Staff("2", "takagi"), new Staff("3", "katoh"), new Staff("4", "shimura")); 7 List<Staff> staffList2 = List.of(new Staff("1", "AAA"), new Staff("2", "BBB")); 8 9 List<Staff> staffList3 = staffList1.stream() 10 .flatMap(i -> staffList2.stream().noneMatch(j -> i.id.equals(j.id)) ? Stream.of(i) : Stream.empty()) 11 .collect(Collectors.toList()); 12 13 staffList3.forEach(System.out::println); 14 } 15 static class Staff{ 16 final String id, name; 17 Staff(String id, String name) { 18 this.id = id; 19 this.name = name; 20 } 21 @Override 22 public String toString() { 23 return String.format("Staff@%x",hashCode())+"[id="+id+",name="+name+"]"; 24 } 25 } 26}
Staff@31befd9f[id=3,name=katoh] Staff@568db2f2[id=4,name=shimura]
投稿2024/09/13 19:10
総合スコア13350
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
0
staffList2 の id だけを抽出した List<String> idList をまず作っておくと idList.contains(id) で存在をチェックできます。
こちらに実行可能なデモを置きました。ご確認ください。
https://paiza.io/projects/tbsVsMmJgQIjXyYkJfqXFQ
java
1import java.util.ArrayList; 2import java.util.Arrays; 3import java.util.List; 4import java.util.stream.Collectors; 5 6public class Main { 7 public static void main(String[] args) { 8 List<Staff> staffList1 = new ArrayList<>(Arrays.asList( 9 new Staff("1", "nakamoto"), 10 new Staff("2", "takagi"), 11 new Staff("3", "katoh"), 12 new Staff("4", "shimura") 13 )); 14 List<Staff> staffList2 = new ArrayList<>(Arrays.asList( 15 new Staff("1", "AAA"), 16 new Staff("2", "BBB") 17 )); 18 System.out.println(staffList1); 19 System.out.println(staffList2); 20 List<String> idList = staffList2.stream() 21 .map(x -> x.getId()) 22 .collect(Collectors.toList()); 23 List<Staff> result = staffList1.stream() 24 .filter(x -> !idList.contains(x.getId())) 25 .collect(Collectors.toList()); 26 System.out.println(result); 27 } 28 29 public static class Staff { 30 private String id; 31 private String name; 32 public Staff(String id, String name) { 33 this.id = id; 34 this.name = name; 35 } 36 public String getId() { 37 return id; 38 } 39 public void setId(String id) { 40 this.id = id; 41 } 42 public String getName() { 43 return name; 44 } 45 public void setName(String name) { 46 this.name = name; 47 } 48 @Override 49 public String toString() { 50 return "Staff{" + 51 "id='" + id + '\'' + 52 ", name='" + name + '\'' + 53 '}'; 54 } 55 } 56} 57 58/* 【実行結果】 59 * [Staff{id='1', name='nakamoto'}, Staff{id='2', name='takagi'}, Staff{id='3', name='katoh'}, Staff{id='4', name='shimura'}] 60 * [Staff{id='1', name='AAA'}, Staff{id='2', name='BBB'}] 61 * [Staff{id='3', name='katoh'}, Staff{id='4', name='shimura'}] 62 */
投稿2024/09/23 01:27
総合スコア86
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
0
staffList1
と staffList2
を結合して id
でグループ化、それぞれの id
に対応するリスト(Staff
クラスのインスタンスのリスト)の内で要素数が 1 のリストを抽出します。
java
1import java.util.Arrays; 2import java.util.List; 3import java.util.stream.Stream; 4import java.util.stream.Collectors; 5 6class Staff{ 7 private final String id; 8 private final String name; 9 10 public Staff(String id, String name) { 11 this.id = id; 12 this.name = name; 13 } 14 15 public String getId() { 16 return this.id; 17 } 18 19 @Override 20 public String toString() { 21 return String.format("Staff{id=\"%s\", name=\"%s\"}", id, name); 22 } 23} 24 25class Main { 26 public static void main(String[] args) { 27 List<Staff> staffList1 = List.of( 28 new Staff("1", "nakamoto"), new Staff("2", "takagi"), 29 new Staff("3", "katoh"), new Staff("4", "shimura")); 30 List<Staff> staffList2 = List.of( 31 new Staff("1", "AAA"), new Staff("2", "BBB")); 32 33 List<Staff> staffList3 = 34 Stream.concat(staffList1.stream(), staffList2.stream()) 35 .collect(Collectors.groupingBy(Staff::getId)) 36 .values().stream().filter(e->e.size()==1) 37 .flatMap(List::stream).collect(Collectors.toList()); 38 39 staffList3.forEach(System.out::println); 40 } 41} 42 43// 実行結果 44// Staff{id="3", name="katoh"} 45// Staff{id="4", name="shimura"}
投稿2024/09/13 17:32
編集2024/09/13 17:36総合スコア21234
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。