Stream API का takewhile() मेथड सभी मानों को तब तक स्वीकार करता है जब तक प्रेडिकेट false नहीं लौटाता जबकि dropWhile() मेथड सभी मानों को तब तक छोड़ देता है जब तक यह प्रेडिकेट से मेल नहीं खाता। यदि स्ट्रीम क्रमबद्ध है, तो takewhile() मेथड इस स्ट्रीम से लिए गए एलिमेंट्स के सबसे लंबे प्रीफिक्स से युक्त स्ट्रीम लौटाता है जो प्रेडिकेट से मेल खाता है जबकि dropWhile() मेथड प्रेडिकेट से मेल खाने के बाद शेष स्ट्रीम लौटाता है। यदि स्ट्रीम अनक्रमित है, तो takewhile() मेथड दिए गए प्रेडिकेट से मेल खाने वाले एलिमेंट्स के सबसेट से युक्त स्ट्रीम लौटाता है जबकि dropWhile() मेथड दिए गए प्रेडिकेट से मेल खाने वाले एलिमेंट्स के सबसेट को छोड़ने के बाद स्ट्रीम के शेष एलिमेंट्स से युक्त स्ट्रीम लौटाता है।
takeWhile() का सिंटैक्स
default StreamtakeWhile(Predicate super T> predicate)
उदाहरण
import java.util.stream.Stream;
public class TakeWhileMethodTest {
public static void main(String args[]) {
Stream.of("India", "Australia", "Newzealand", "", "South Africa", "England")
.takeWhile(o->!o.isEmpty())
.forEach(System.out::print);
}
}
आउटपुट
IndiaAustraliaNewzealand
dropWhile() का सिंटैक्स
default StreamdropWhile(Predicate super T> predicate)
उदाहरण
import java.util.stream.Stream;
public class DropWhileMethodTest {
public static void main(String args[]) {
Stream.of("India", "Australia", "Newzealand", "", "England", "Srilanka")
.dropWhile(o->!o.isEmpty())
.forEach(System.out::print);
System.out.println();
Stream.of("India", "", "Australia", "", "England", "Srilanka")
.dropWhile(o->!o.isEmpty())
.forEach(System.out::print);
}
}
आउटपुट
EnglandSrilanka AustraliaEnglandSrilanka