1
votes

The code listing:

protected List<R> getRows(String startDate, String endDate, Function<MapListHandler, R> func){

    ConnectionManager cm = new ConnectionManager();
    List<R> rows = null;
    try(Connection c = cm.getConnection()) {

        String sql = getSql();

        rows = new QueryRunner()
                .query(c, sql, new MapListHandler(), startDate, endDate, startDate, endDate)
                .stream()
                .map(func).collect(Collectors.toList());

    } catch (SQLException e) {
        e.printStackTrace();
    }
    finally {
        cm.closeConnection();
    }

    return rows;
}

fails to compile with the following message:

method map in interface java.util.stream.Stream<T> cannot be applied to given types; [ERROR] required: java.util.function.Function<? super java.util.Map<java.lang.String,java.lang.Object>,? extends R> [ERROR] found: java.util.function.Function<org.apache.commons.dbutils.handlers.MapListHandler,R> [ERROR] reason: cannot infer type-variable(s) R [ERROR] (argument mismatch; java.util.function.Function<org.apache.commons.dbutils.handlers.MapListHandler,R> cannot be converted to java.util.function.Function<? super java.util.Map<java.lang.String,java.lang.Object>,? extends R>)

1
The code compiles if I use with Map<String, Object> instead of MapHanlderkostas
Sure, it compiles with Function<Map<String, Object>, R> func because Map<String, Object> is the type of the Stream elements. What's your question?Stefan Zobel

1 Answers

1
votes

A MapListHandler will cause query to return a List<Map<String, Object>>.

This means that stream() will return a Stream<Map<String, Object>>. Its map() method thus expects a Function<? super Map<String, Object>, ? extends R>.

Of course a MapListHandler is not a super type of Map<String, Object> so func is not a valid parameter for map().