2つのストリームが互いに素であるかどうかを確認するにはどうすればよいですか?

Nov 23 2020

ストリームと比較して、共通の要素が1つ以上あるかどうかを確認したいと思います(1つを見つけるだけで、それ以上の要素を探すのをやめることができます)。これを、カスタム作成されたクラスを含むStreamsに適用できるようにしたいと思います。

説明のために、次のようなクラスがあるとします。

public class Point {
    public final int row;
    public final int col;

    public Point(int row, int col) {
        this.row = row;
        this.col = col;
    }    

    @Override
    public boolean equals(Object obj) {
        if (obj == null) return false;
        if (obj.getClass() != this.getClass()) return false;
        final Point other = (Point) obj;
        return this.row == other.row && this.col == other.col;
    }

    @Override
    public int hashCode() {
        return Objects.hash(row, col); 
    }
}

そして、次のような2つの素敵なストリームがあります。

Stream<Point> streamA = Stream.of(new Point(2, 5), new Point(3, 1));
Stream<Point> streamB = Stream.of(new Point(7, 3), new Point(3, 1));

これらのストリームに共通の1ポイント(つまり、Point(3, 1))があることを考えると、最終結果を真にしたいと思います。

必要な機能は次のように表すことができます。

public static boolean haveSomethingInCommon(Stream<Point> a, Stream<Point> b){
    //Code that compares a and b and returns true if they have at least 1 element in common
}

回答

1 YCF_L Nov 23 2020 at 14:37

まず、有名なエラーが発生しないように、ストリームをセットまたはリストに変換する必要があります。

java.lang.IllegalStateException: stream has already been operated upon or closed

そして、あなたはanyMatchこれとして使うことができます:

public static boolean haveSomethingInCommon(Stream<Coord> a, Stream<Coord> b) {
    Set<Coord> setA = a.collect(Collectors.toSet());
    Set<Coord> setB = b.collect(Collectors.toSet());

    return setA.stream().anyMatch(setB::contains);
}

または、bストリームのみをセットに変換して使用することもできます。

public static boolean haveSomethingInCommon(Stream<Coord> a, Stream<Coord> b) {
    Set<Coord> setB = b.collect(Collectors.toSet());
    return a.anyMatch(setB::contains);
}

メソッドのparamとしてではSet<Coord>なく、することをお勧めStream<Coord>します。

public static boolean haveSomethingInCommon(Set<Coord> a, Set<Coord> b) {
    return a.stream().anyMatch(b::contains);
}
2 Naman Nov 23 2020 at 16:17

2つのストリームを個別に収集しなくても、複数の値が任意のキーにマップされているかどうかをグループ化して識別することができます。

public static boolean haveSomethingInCommon(Stream<Coord> a, Stream<Coord> b) {
    return Stream.concat(a, b)
            .collect(Collectors.groupingBy(Function.identity()))
            .values().stream()
            .anyMatch(l -> l.size() > 1);
}

同じストリームに同じ要素を2回以上含めることができる場合は、使用するコードを変更できます-

Stream.concat(a.distinct(), b.distinct())
Kaplan Nov 23 2020 at 18:19

機能がある disjoint では Collections

public static boolean haveSomethingInCommon( Stream<Coord> a, Stream<Coord> b ) {
  return( ! Collections.disjoint( a.collect( toList() ), b.collect( toList() ) ) );
}