अगर दो धाराएं असंतुष्ट हैं तो कैसे जांच करें?

Nov 23 2020

मैं धाराओं की तुलना करना चाहूंगा, और जांच करूंगा कि क्या उनके पास 1 या अधिक तत्व आम हैं (खोजने के लिए 1 अधिक ढूंढना बंद करने के लिए पर्याप्त है)। मैं इसे कस्टम-निर्मित वर्ग वाली धाराओं में लागू करने में सक्षम होना चाहता हूं।

उदाहरण के लिए, मान लें कि मेरे पास एक वर्ग है जो दिखता है:

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); 
    }
}

और फिर मेरे पास दो प्यारी धाराएँ हैं जो इस तरह दिखती हैं:

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);
}

मैं आपकी विधि में परम के 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

स्वतंत्र रूप से दो धाराओं को इकट्ठा किए बिना, आप समूह कर सकते हैं और पहचान सकते हैं कि क्या किसी मान के लिए कई मान मैप किए गए हैं।

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);
}

यदि एक ही स्ट्रीम में दो या अधिक समान तत्व हो सकते हैं, तो आप उपयोग करने के लिए कोड बदल सकते हैं -

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() ) ) );
}