How would be the right way to do something like this in Java:
String a = "abc" + () -> { return "def";};
I understand the above code have no purpose, but it represents my real problem.
EDIT
first post here, intent to get and provide some help ;)
I'm actually trying to concatenate the return value of the function
yshavit - Keeping in mind that a function the value it produces are separate concepts -- and if they weren't, you wouldn't need two constructs for them
many thanks, i didn't really understood that
The real problem is that I don't (or shouldn't) need to write a method "b" for returning this string because it will be executed only once every time method "a" is called. Also it will not even take parameters.
Below is the real code, to help understanding:
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
final Connection connection;
final Statement statement;
final ResultSet result;
final ResultSetMetaData meta;
final int cols;
final String[] labels;
final HttpSession session = request.getSession(false);
final String table = request.getParameter("table");
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(
"<!DOCTYPE html>"
+ "<html>"
+ element("header.html")
+ "<body>"
+ "<nav class=\"sidepane\">"
+ element("nav_hub.html")
//---------------------------------------------------here
+ () -> { //where i'm trying to concatenate
try {
Class.forName("com.mysql.jdbc.Driver");
connection = (Connection) session.getAttribute("connection");
statement = connection.createStatement();
result = statement.executeQuery("SELECT * FROM " + table);
meta = result.getMetaData();
cols = meta.getColumnCount();
labels = new String[cols];
// ESCREVE A TABELA
writer.write("<thead><tr>"); //header
for (int i = 0; i < cols; i++) {
labels[i] = meta.getColumnLabel(i + 1);
writer.write("<th>" + labels[i].toUpperCase() + "</th>");
}
writer.write("</tr></thead><tbody>"); //body
while (result.next()) {
writer.write("<tr>");
for (String column : labels) {
writer.write("<td");
if (meta.isWritable(1)) {writer.write(" contenteditable='true' ");}
writer.write(">" + result.getString(column) + "</td>");
}
writer.write("</tr>");
}
writer.write("</tbody>");
} catch (SQLException | ClassNotFoundException e) {
writer.write("erro ao ler banco de dados: " + e.getMessage());
}
}
//--------------------------------------------------------
+ "</nav>"
+ "</body>"
+ "</html>"
);
}
Now, with yshavit answer I understood that I can't use the lambda as the returned value. Still I don't know how would I use the return of that lambda and concatenate to the other strings (without creating a method).
Obs.: if you're wondering, there is a reason for not using a jsp.