Since I’m using JDK 1.8 the below code works file on my local. But this will fail in deployment as VM uses JDK 1.7 and doesn’t understand stream.
public static String buildQuery(Map<String, String> dimensions, Map<String, String> measures,
List<Constraint> constraints, final RequestMode requestMode, String queryMode, String reportName,
String table) {
queryMode = StringUtils.isEmpty(queryMode) ? "sync" : queryMode;
final List<FilterDefinition> filters;
if (CollectionUtils.isNotEmpty(constraints)) {
filters = constraints.stream().map(FilterDefinition::new).collect(Collectors.toList());
} else {
filters = new ArrayList<>();
}
final QueryDefinition queryDefinition = new QueryDefinition(createColumnList(dimensions),
createColumnList(measures), filters, requestMode.name(), table);
return gson.toJson(new Request(queryMode, reportName, queryDefinition));
}
How to modify this to get the same output, but without stream?
>Solution :
Ok, step-by-step:
-
stream()we have to convert into afor/whileloop:so:
constraints.stream()...gets to:
for (Constraint c : constraints) { ... } -
The
map()we "convert" inside the loop. For eachConstraintit should construct us anew FilterDefinition(c))so:
...map(FilterDefinition::new)...gets to:
... { ... (new FilterDefinition(c)) ...} -
The
collect(toList())we accomplish by creating and (consequently) adding to our list.so:
.collect(Collectors.toList())gets to:
filters = new ArrayList<>(); for ... { filters.add(...); }
Combining it all, should get us:
...
final List<FilterDefinition> filters = new ArrayList<>();
if (CollectionUtils.isNotEmpty(constraints)) {
for (Constraint c : constraints) {
filters.add(new FilterDefinition(c));
}
} // when we initialized the list already, we don't need the "else"!