Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map stream of Data into Page<T>

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.

like image 230
ramden Avatar asked Oct 22 '18 13:10

ramden


People also ask

How to convert map to stream in Java?

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.

What are the examples of stream 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.

How to do value stream mapping?

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.

How to convert a stream to a map using collectors?

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:


1 Answers

PageImpl has constructor from list

Page<SomeType> controllerMethod() {

    List<SomeType> allItems = someRepository.findAll()
                              .filter(something)
                              .collect(Collectors.toList())

    return new PageImpl<>(allItems);
}
like image 153
Estorsky Avatar answered Sep 24 '22 19:09

Estorsky