A Stream takeWhile() is introduce in Java 9. It takes Stream elements until the specified predicate evaluated as true that can be either ordered or unordered. Once predicate evaluated as true, it will not evaluate for remaining elements of stream. It is Stream intermediate operation that return a Stream object after performing operation.
1. | ordered : It returns a longest prefix of elements taken from the stream that match the predicate starting from beginning of the stream. |
2. | un-ordered : It returns a subset of the stream elements that match the given predicate (but not all), starting at the beginning of the stream. |
1 default Stream<T> takeWhile(Predicate<T> predicate)
T : It represents a type of Stream elements.
predicate : It is predicate object that returns a boolean value and apply to the stream elements.
1 package com.java;
2
3 import java.util.Arrays;
4 import java.util.List;
5 import java.util.stream.Collectors;
6
7 public class Example {
8 public static void main(String[] args) {
9
10 List<String> alphabets = Arrays.asList("a", "b", "c", "d", "e", "f");
11
12 List<String> subset1 = alphabets.stream()
13 .takeWhile(s -> !s.equals("d"))
14 .collect(Collectors.toList());
15 System.out.println("Elements : " + subset1);
16 }
17 }
In the above example, a list object is stream using stream() method. A takWhile() called by passing a predicate that returns boolean value. It takes stream elements until an element value is equals to d. It returns stream ordered elements starting from first elements. Once predicate evaluated as true, it will not evaluate for remaining elements of stream.
Stream method takeWhile() with Integer
Stream method takeWhile() with Integer
1 package com.java;
2
3 import java.util.Arrays;
4 import java.util.List;
5 import java.util.stream.Collectors;
6
7 public class Example {
8 public static void main(String[] args) {
9
10 List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 3, 7);
11
12 List<Integer> subset1 = numbers.stream()
13 .takeWhile(s -> s < 5)
14 .collect(Collectors.toList());
15 System.out.println("Elements : " + subset1);
16 }
17 }
In the above example, a takeWhile() method is called by specifying a predicate that validate whether element value is less than 5. Once it encounter element value which is not less than 5, it will not check remaining elements. The resulting elements collected by collect() method and convert it to list and return.
1 Elements : [1, 2, 3, 4]
takeWhile() with object
1 package com.java;
2
3 import java.util.Arrays;
4 import java.util.List;
5 import java.util.stream.Collectors;
6
7 public class OrderExp {
8 public static void main(String[] args) {
9 Product pd1 = new Product(100, "Phone", 10, 120000.32);
10 Product pd2 = new Product(101, "TV", 7, 48730.51);
11 Product pd3 = new Product(102, "Laptop", 10, 439482.05);
12 Product pd4 = new Product(103, "Watch", 7, 10000.44);
13
14 List<Product> products = Arrays.asList(pd1, pd2, pd3, pd4);
15
16 List<Product> res = products.stream()
17 .takeWhile(p -> p.getPid() < 102)
18 .collect(Collectors.toList());
19
20 System.out.println("Elements : " + res);
21 }
22 }
In the above example, a list of products are created and stream using collection stream() method which will returns stream object. A stream method takeWhile() method called that stream elements until value of product ID is less than 102. A collect() method collect elements and converts to list using Collectors toList() method and prints the result.
1 Elements : [Phone 10 120000.32, TV 7 48730.51]
Related options for your search