For quite a while now I am trying to get following to work in my resource / controller:
Have a stream of data and then map the stream of data to a Page response
Page<SomeType> controllerMethod() {
List<SomeType> allItems = someRepository.findAll()
.filter(something)
.collect(Collectors.toList())
return allItems.map(...?)
}
Question:
Are there any helpers from spring that can help me acheive this?
It would be ok that the paging is not done on DB level.
A Stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. Below are various method to convert Map to Stream in Java: Converting complete Map<Key, Value> into Stream: This can be done with the help of Map.entrySet () method which returns a Set view of the mappings contained in this map.
Example 1 : Stream map () function with operation of number * 3 on each element of stream. Example 2 : Stream map () function with operation of converting lowercase to uppercase.
Now that you know the basics of value stream mapping, here are the exact steps you’d need to take to carry out the initiative… Typically, you would start your mapping by indicating a start and end point. This would show where your internal process begins and ends. Some companies, however, prefer to map out the entire value chain.
The Collectors.toMap () method takes two parameters as the input: KeyMapper: This function is used for extracting keys of the Map from stream value. ValueMapper: This function used for extracting the values of the map for the given key. The following are the examples of the toMap function to convert the given stream into a map:
PageImpl has constructor from list
Page<SomeType> controllerMethod() {
List<SomeType> allItems = someRepository.findAll()
.filter(something)
.collect(Collectors.toList())
return new PageImpl<>(allItems);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With