Package java.util.stream
The ‘filter’ method is used to eliminate elements based on criteria.
The sorted method is used to sort the stream.
The reduce method is used to reduce the elements of a stream to a single value.
Java 8 introduced the concept of stream, Stream represents a sequence of objects from a source, which supports aggregate operations.
Following are the characteristics of a Stream −
- It takes input from the Collections, Arrays, or I/O channels.
- It never stores the elements.
- Stream supports intermediate operations like filter, map, limit, reduce, find, match, and so on.
- Streams only provide the result as per the pipelined methods.
- Stream operations do the iterations internally over the source elements provided.
Different Operations On Streams-
forEach
The stream has provided a new method ‘forEach’ to iterate each element of the stream
forEach
The stream has provided a new method ‘forEach’ to iterate each element of the stream
List<Integer> obj = new ArrayList<>();
obj.add(7);
obj.add(12);
obj.add(3);
obj.add(5);
obj.forEach((o) -> System.out.println("Item : " + o));
map
The map method is used to map the items in the collection to other objects according to the Function passed as the argument.
filterThe map method is used to map the items in the collection to other objects according to the Function passed as the argument.
List<String> obj = Arrays.asList("h", "bh", "ca", "d");
List<String> collectUpperCaseLetters = obj.stream().map(String::toUpperCase).collect(Collectors.toList());
System.out.println(collectUpperCaseLetters);
List<String> collectUpperCaseLetters = obj.stream().map(String::toUpperCase).collect(Collectors.toList());
System.out.println(collectUpperCaseLetters);
The ‘filter’ method is used to eliminate elements based on criteria.
List<String> names = Arrays.asList("Sibin", "Rahul", "Jose");
List<String> filteredNames = names.stream().filter(s -> s.startsWith("S")).collect(Collectors.toList());
System.out.println(filteredNames);
sortedList<String> filteredNames = names.stream().filter(s -> s.startsWith("S")).collect(Collectors.toList());
System.out.println(filteredNames);
The sorted method is used to sort the stream.
List<String> names = Arrays.asList("Sibin", "Rahul", "Jose");
List<String> result = names.stream().sorted().collect(Collectors.toList());
System.out.println(result);
reduceList<String> result = names.stream().sorted().collect(Collectors.toList());
System.out.println(result);
The reduce method is used to reduce the elements of a stream to a single value.
int[] array = {2,4,5,7,2};
Arrays.stream(array).reduce((x,y) -> x+y).ifPresent(s -> System.out.println(s));
Program to reveal the use of StreamArrays.stream(array).reduce((x,y) -> x+y).ifPresent(s -> System.out.println(s));
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class StreamTest {
public static void main(String[] args) {
// demonstration of forEach method
List<Integer> obj = new ArrayList<>();
obj.add(7);
obj.add(12);
obj.add(3);
obj.add(5);
obj.forEach((o) -> System.out.println(o));
/* End */
// demonstration of map method
List<String> alplalist = Arrays.asList("h", "bh", "ca", "d");
List<String> collectUpperCaseLetters = alplalist.stream().map(String::toUpperCase).collect(Collectors.toList());
System.out.println(collectUpperCaseLetters);
/* End */
// demonstration of filter method
List<String> names = Arrays.asList("Sibin", "Rahul", "Jose");
List<String> filteredNames = names.stream().filter(s -> s.startsWith("S")).collect(Collectors.toList());
System.out.println(filteredNames);
/* End */
// demonstration of sorted method
List<String> namelist = Arrays.asList("Sibin", "Rahul", "Jose");
List<String> result = namelist.stream().sorted().collect(Collectors.toList());
System.out.println(result);
/* End */
// demonstration of reduce method
int[] array = { 2, 4, 5, 7, 2 };
Arrays.stream(array).reduce((x, y) -> x + y).ifPresent(s -> System.out.println(s));
/* End */
}
}
Output:
7
12
3
5
[H, BH, CA, D]
[Sibin]
[Jose, Rahul, Sibin]
20
12
3
5
[H, BH, CA, D]
[Sibin]
[Jose, Rahul, Sibin]
20
This article is contributed by Knf Team. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Genuine and Free jobs!
ReplyDeleteDon't miss any job opportunities!
Improve your life, only one step!
More Java jobs info, Please join the Java Jobs group:
https://chat.whatsapp.com/HLFm93tkHQR4zu8hafpqSl