I have a query like this that seems complicated and I want to do it by using Java Stream Api
First off, the SQL query you are generating probably doesn't do what you want it to do. Because you using a subquery (a SELECT inside a SELECT) you are first limiting the unsorted data, and the sorting it, resulting in returning &quot;random&quot; rows instead of the first rows in sorted order. There is no reason to use a subquery, so instead of
SELECT * FROM (SELECT * FROM user_order WHERE ... LIMIT ?, ?) a ORDER BY ...

use
SELECT * FROM user_order WHERE ... ORDER BY ... LIMIT ?, ?


Then you really should format your code cleanly. The indentation is really bad and you should insert some empty lines between logical code blocks to separate them optically. Also put spaces in lines, specifically around operators, between keywords (if, while etc.) and (, after ; and before {. For example, the line:
for(int i=0;i&lt;values.size();i++){

should look like this:
for (int i = 0; i &lt; values.size(); i++) {

BTW, your editor/IDE should be able to do that for you. Look for a menu item &quot;Format code&quot;.

Don't declare variables until you need them. Both resultSet and orders don't need to be declared until the loop at the end:
ResultSet resultSet = statement.executeQuery();
List&lt;Order&gt; orders = new ArrayList&lt;&gt;();
while (resultSet.next()){