Source:
<code>public Map<Long, UserPreference> getUsersPreference(String preferenceType, String preferenceName, List<RegisteredUserDTO> users) throws SegueDatabaseException { Validate.notBlank(preferenceType); Validate.notBlank(preferenceName); Map<Long, UserPreference> usersPreferenceMap = Maps.newHashMap(); int pageSize = 10000; int fromIndex = 0; int toIndex = min(pageSize, users.size()); while (fromIndex < toIndex) { List<RegisteredUserDTO> pagedUsers = users.subList(fromIndex, toIndex); try (Connection conn = database.getDatabaseConnection()) { PreparedStatement pst; StringBuilder sb = new StringBuilder(); sb.append("SELECT * FROM user_preferences WHERE user_id IN ("); for (int i = 0; i < pagedUsers.size(); i++) { sb.append("?").append(i < pagedUsers.size() - 1 ? ", " : ""); } sb.append(") AND preference_type=? AND preference_name=? ORDER BY user_id ASC;"); pst = conn.prepareStatement(sb.toString()); for (int i = 1; i <= pagedUsers.size(); i++) { pst.setLong(i, pagedUsers.get(i - 1).getId()); } pst.setString(pagedUsers.size() + 1, preferenceType); pst.setString(pagedUsers.size() + 2, preferenceName); ResultSet results = pst.executeQuery(); while (results.next()) { Long userId = results.getLong("user_id"); <START> UserPreference pref = new UserPreference(userId, results.getString("preference_type"), <END> results.getString("preference_name"), results.getBoolean("preference_value")); usersPreferenceMap.put(userId, pref); } fromIndex = toIndex; toIndex = min(toIndex + pageSize, users.size()); } catch (SQLException e) { throw new SegueDatabaseException("Postgres exception", e); } } return usersPreferenceMap; }</code><technical_language>construction of objects refactored out in a common method? repeated string literals some problems if need change</technical_language>

Target:
public Map<Long, UserPreference> getUsersPreference(String preferenceType, String preferenceName, List<RegisteredUserDTO> users) throws SegueDatabaseException { Validate.notBlank(preferenceType); Validate.notBlank(preferenceName); Map<Long, UserPreference> usersPreferenceMap = Maps.newHashMap(); int pageSize = 10000; int fromIndex = 0; int toIndex = min(pageSize, users.size()); while (fromIndex < toIndex) { List<RegisteredUserDTO> pagedUsers = users.subList(fromIndex, toIndex); try (Connection conn = database.getDatabaseConnection()) { PreparedStatement pst; StringBuilder sb = new StringBuilder(); sb.append("SELECT * FROM user_preferences WHERE user_id IN ("); for (int i = 0; i < pagedUsers.size(); i++) { sb.append("?").append(i < pagedUsers.size() - 1 ? ", " : ""); } sb.append(") AND preference_type=? AND preference_name=? ORDER BY user_id ASC;"); pst = conn.prepareStatement(sb.toString()); for (int i = 1; i <= pagedUsers.size(); i++) { pst.setLong(i, pagedUsers.get(i - 1).getId()); } pst.setString(pagedUsers.size() + 1, preferenceType); pst.setString(pagedUsers.size() + 2, preferenceName); ResultSet results = pst.executeQuery(); while (results.next()) { Long userId = results.getLong("user_id"); UserPreference pref = userPreferenceFromResultSet(results); usersPreferenceMap.put(userId, pref); } fromIndex = toIndex; toIndex = min(toIndex + pageSize, users.size()); } catch (SQLException e) { throw new SegueDatabaseException("Postgres exception", e); } } return usersPreferenceMap; }