I have a query like this that seems complicated and I want to do it by using Java Stream Api I have method that fetches sorted and filtered data from Mysql Database, Firstly I had done It by using Java. Now I did it by Mysql but I don't like the way the code looks
I have entity that named Order and It looks like this
public class Order implements Serializable {
    private int orderId;
    private int userId;
    private int carId;
    private String userAddress;
    private String userDestination;
    private double orderCost;
    @JsonSerialize(using = CustomLocalDateTimeJsonSerializer.class)
    private LocalDateTime orderDate;
//getters, setters
}

Method that performs all work
@Override
    public List&lt;Order&gt; findFilSortOrders(Map&lt;String, String&gt; filters, String sortedColumn, boolean descending, int startRow, int rowsPerPage) {
    StringBuilder query = new StringBuilder(&quot;SELECT*FROM (SELECT*FROM user_order &quot;);
    ResultSet resultSet = null;
    LinkedHashMap&lt;String,String&gt; filterToQuery;
    List&lt;String&gt; values = new ArrayList&lt;&gt;();
    List&lt;String&gt; keys = new ArrayList&lt;&gt;();
        List&lt;Order&gt; orders = new ArrayList&lt;&gt;();
        if(filters!=null){
            query.append(&quot;WHERE &quot;);
            filterToQuery = new LinkedHashMap&lt;&gt;(filters);
            values = new ArrayList&lt;&gt;(filterToQuery.values());
            keys = new ArrayList&lt;&gt;(filterToQuery.keySet());
            keys.forEach(key-&gt;
        {
            if(key.equals(&quot;orderDate&quot;)){
                query.append(&quot;date_format(&quot;).append(key).append(&quot;,'%Y-%m-%d %H:%i') = ? AND &quot;);
            }else{
                query.append(key).append(&quot; = ? AND &quot;);
            }
        });
            query.setLength(query.length()-4);
    }
        query.append(&quot; LIMIT ?, ?) a&quot;).append(&quot; ORDER BY &quot;).append(sortedColumn).append(&quot; &quot;).append(descending?&quot;DESC&quot;:&quot;ASC;&quot;);
    try(Connection connection = MySQLDAOFactory.getConnection();
    PreparedStatement statement = connection.prepareStatement(query.toString())){
       for (int i=0;i&lt;values.size();i++){
           switch (keys.get(i)){
               case &quot;orderId&quot;:
               case &quot;userId&quot;:
               case &quot;carId&quot;:{
                   statement.setInt(i+1, Integer.parseInt(values.get(i)));
                   break;
               }
               case &quot;orderDate&quot;:
               case &quot;userAddress&quot;:
               case &quot;userDestination&quot;:{
                  statement.setString(i+1,values.get(i));
                   break;
               }
               case &quot;orderCost&quot;:{
                   statement.setDouble(i+1,Double.parseDouble(values.get(i)));
                   break;
               }
           }
       }
       statement.setInt(values.size()+1,startRow);
       statement.setInt(values.size()+2,rowsPerPage);
        resultSet = statement.executeQuery();
        while(resultSet.next()){
            Order order = new Order();
            order.setCarId(resultSet.getInt(&quot;carId&quot;));
            order.setOrderId(resultSet.getInt(&quot;orderId&quot;));
            order.setUserId(resultSet.getInt(&quot;userId&quot;));
            order.setUserAddress(resultSet.getString(&quot;userAddress&quot;));
            order.setUserDestination(resultSet.getString(&quot;userDestination&quot;));
            order.setOrderCost(resultSet.getDouble(&quot;orderCost&quot;));
            order.setOrderDate(resultSet.getTimestamp(&quot;orderDate&quot;).toLocalDateTime());
            orders.add(order);
        }
    }catch (SQLException e){
        LOGGER.error(e);
        try {
            if (resultSet != null) {
                resultSet.close();
            }
        } catch (SQLException throwables) {
            LOGGER.error(throwables);
        }
    }
     return orders;
    }

This code works but I dont like it looks If you could help me improve the code, I would be very grateful. Thanks for answering.
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()){