I am creating SQL SELECT query String, by taking two lists and iterating over them in parallel.
List<String> selectFields = new ArrayList<>();
List<String> uuidFunction = new ArrayList<>();
selectFields.add("id");
selectFields.add("name");
uuidFunction.add("UUID_TO_STR");
uuidFunction.add(null);
StringBuilder sql = new StringBuilder("SELECT ");
IntStream.range(0, selectFields.size())
.parallel()
.forEach(i -> {
sql.append(
uuidFunction.get(i) != null ? uuidFunction.get(i) + "(" + selectFields.get(i) + ")"
: selectFields.get(i));
}
);
sql.append(" FROM test_table");
This gives me a nice SQL String BUT without commans ,
between each element.
SELECT nameUUID_TO_STR(id)
Why can't I use .collect(Collectors.joining(","))
right after forEach ?
NOTE this problem is part of a bigger function in my Project where are are not using direct SQL Prepared Statements. So please don't provide SQL way of solving this problem. This is a generic problem related to iterations and appending Strings.