How to use Java 8 lambda features to get Contact Model in Spring MVC Spring JDBC example?
<p>I was working on <code>Spring MVC + Spring JDBC</code> example. Also I'm using <code>Java 8</code>. In this example I am facing issues in converting <code>new ResultSetExtractor&lt;Contact&gt;()</code> to use Java 8's lambda features like I was able to do that in case of <code>List&lt;Contact&gt;</code> using <code>this::mapContact</code>. How we can do that for <code>public Contact get(int contactId)</code>?</p>

<p><strong>ContactDAOImpl.java</strong></p>

<pre><code>public class ContactDAOImpl implements ContactDAO {

    private JdbcTemplate jdbcTemplate;

    public ContactDAOImpl(DataSource dataSource) {
        jdbcTemplate = new JdbcTemplate(dataSource);
    }

    @Override
    public void saveOrUpdate(Contact contact) {
        if (contact.getId() &gt; 0) {
            // update
            String sql = "UPDATE contact SET name=?, email=?, address=?, telephone=? WHERE contact_id=?";

            jdbcTemplate.update(sql, contact.getName(), contact.getEmail(), contact.getAddress(),
                    contact.getTelephone(), contact.getId());
        } else {
            // insert
            String sql = "INSERT INTO contact (name, email, address, telephone) VALUES (?, ?, ?, ?)";
            jdbcTemplate.update(sql, contact.getName(), contact.getEmail(), contact.getAddress(),
                    contact.getTelephone());
        }

    }

    @Override
    public void delete(int contactId) {
        String sql = "DELETE FROM contact WHERE contact_id=?";
        jdbcTemplate.update(sql, contactId);
    }

    @Override
    public List&lt;Contact&gt; list() {
        String sql = "SELECT * FROM contact";
        List&lt;Contact&gt; listContact = jdbcTemplate.query(sql, this::mapContact);

        return listContact;
    }

    @Override
    public Contact get(int contactId) {
        String sql = "SELECT * FROM contact WHERE contact_id=" + contactId;
        return jdbcTemplate.query(sql, new ResultSetExtractor&lt;Contact&gt;() {

            @Override
            public Contact extractData(ResultSet rs) throws SQLException, DataAccessException {
                if (rs.next()) {
                    Contact contact = new Contact();
                    contact.setId(rs.getInt("contact_id"));
                    contact.setName(rs.getString("name"));
                    contact.setEmail(rs.getString("email"));
                    contact.setAddress(rs.getString("address"));
                    contact.setTelephone(rs.getString("telephone"));
                    return contact;
                }

                return null;
            }

        });
    }


    private Contact mapContact(ResultSet rs, int row) throws SQLException{
        Contact contact = new Contact();
        contact.setId(rs.getInt("contact_id"));
        contact.setName(rs.getString("name"));
        contact.setEmail(rs.getString("email"));
        contact.setAddress(rs.getString("address"));
        contact.setTelephone(rs.getString("telephone"));
        return contact;
    }
}
</code></pre>

<p>When I changes above to below it looks like I can't use lamba expression here.</p>

<pre><code>public class ContactDAOImpl implements ContactDAO {

    private JdbcTemplate jdbcTemplate;

    public ContactDAOImpl(DataSource dataSource) {
        jdbcTemplate = new JdbcTemplate(dataSource);
    }

    @Override
    public void saveOrUpdate(Contact contact) {
        if (contact.getId() &gt; 0) {
            // update
            String sql = "UPDATE contact SET name=?, email=?, address=?, telephone=? WHERE contact_id=?";

            jdbcTemplate.update(sql, contact.getName(), contact.getEmail(), contact.getAddress(),
                    contact.getTelephone(), contact.getId());
        } else {
            // insert
            String sql = "INSERT INTO contact (name, email, address, telephone) VALUES (?, ?, ?, ?)";
            jdbcTemplate.update(sql, contact.getName(), contact.getEmail(), contact.getAddress(),
                    contact.getTelephone());
        }

    }

    @Override
    public void delete(int contactId) {
        String sql = "DELETE FROM contact WHERE contact_id=?";
        jdbcTemplate.update(sql, contactId);
    }

    @Override
    public List&lt;Contact&gt; list() {
        String sql = "SELECT * FROM contact";
        List&lt;Contact&gt; listContact = jdbcTemplate.query(sql, this::mapContact);

        return listContact;
    }

    @Override
    public Contact get(int contactId) {
        String sql = "SELECT * FROM contact WHERE contact_id=" + contactId;
        return jdbcTemplate.query(sql, this::mapContact);
    }


    private Contact mapContact(ResultSet rs, int row) throws SQLException{
        Contact contact = new Contact();
        contact.setId(rs.getInt("contact_id"));
        contact.setName(rs.getString("name"));
        contact.setEmail(rs.getString("email"));
        contact.setAddress(rs.getString("address"));
        contact.setTelephone(rs.getString("telephone"));
        return contact;
    }
}
</code></pre>

<p>Whats the replacement of below in Java 8 ?</p>

<pre><code>return jdbcTemplate.query(sql, new ResultSetExtractor&lt;Contact&gt;() {

            @Override
            public Contact extractData(ResultSet rs) throws SQLException, DataAccessException {
                if (rs.next()) {
                    Contact contact = new Contact();
                    contact.setId(rs.getInt("contact_id"));
                    contact.setName(rs.getString("name"));
                    contact.setEmail(rs.getString("email"));
                    contact.setAddress(rs.getString("address"));
                    contact.setTelephone(rs.getString("telephone"));
                    return contact;
                }

                return null;
            }

        });
</code></pre>