Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Trying to implement the below code without using Stream

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Ok, step-by-step:

  1. stream() we have to convert into a for/while loop:

    so:

    constraints.stream()...
    

    gets to:

    for (Constraint c : constraints) { ... }
    
  2. The map() we "convert" inside the loop. For each Constraint it should construct us a new FilterDefinition(c))

    so:

    ...map(FilterDefinition::new)...
    

    gets to:

    ... { ... (new FilterDefinition(c)) ...}
    
  3. 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"!
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading