2

I have a Class like this to store APIResponse from REST-API call

class DataSet {
        public String ID;    
        List<DataPoint> dataPoints;
}

class DataPoint{
       DateTime timeStamp;
       Double value;
}

What I have is an ArrayList of DataSet and need to transform into something like this

[{x: timestamp1, y: value1}, {x: timestamp2, y: value2},... {x: timestampn, y: valuen}]

for each element in the ArrayList.

I am learning Stream API of Java 8 and wish to do it using the same. Would like to know how to arrive at the output using lambda expressions of Stream API java 8.

Currently the desired output is being acheived by traditional way of using for-each which is:

List<ArrayList<Map<String, Object>>> transformedObject = new List<ArrayList<Map<String, Object>>>();
        for(DataSet m : apiResponseArray){  
            ArrayList<Map<String, Object>> dataobj = new ArrayList<Map<String, Object>>();      
            List<DataPoint> listData =m.getdataPoints();
            for(DataPoint d : listData)
            {
                Map<String, Object> dataMap = new LinkedHashMap<String, Object>();                  
                dataMap.put("x", d.getTime(););
                dataMap.put("y", d.getValue());
                dataobj.add(dataMap);
            }
            transformedObject.add(dataobj);         
        }
Gson gson = new Gson();
return gson.toJson(transformedObject);

Thanks in advance!!!

13
  • You could flat map apiResponseArray, but I doubt it'll improve your code significantly. Commented Mar 16, 2017 at 18:20
  • @shmosel how can i make the stream to return a list of arraylists of maps that way? Commented Mar 16, 2017 at 18:26
  • @shmosel what else would u suggest to improve performance here? Commented Mar 16, 2017 at 18:27
  • Was there a performance issue? You didn't mention any. Commented Mar 16, 2017 at 18:28
  • @shmosel Yes its taking quite a bit of time for the conversion.Since the response is huge, i thought using streams might help. Pardon me if my thinking isnt right. Commented Mar 16, 2017 at 18:32

1 Answer 1

3

One possible way with inner stream (if you really want list of list of map :) )

private Map<String, Object> map(DataPoint dataPoint) {
    Map<String, Object> map = new LinkedHashMap<>();
    map.put("x", dataPoint.timeStamp);
    map.put("y", dataPoint.value);
    return map;
}


List<List<Map<String, Object>>> transformedObject = dataSets.stream()
            .map(
                    dataSet -> dataSet.dataPoints.stream()
                        .map(this::map)
                        .collect(Collectors.toList())
                )
            .collect(Collectors.toList());
Sign up to request clarification or add additional context in comments.

4 Comments

what is your take on the performance improvement through this approach, as opposed to the traditional approach?
@BabyHulk There is no performance impact, but you did not mention it in your original question. You wanted to rewrite it to streams, only.
Probably, the loop is not bottleneck, but serialization itself as @shmosel wrote. Of course you can use parallel streams, but I have doubts about improving performance in your case.
But Interestingly though certainly not expected, streams is giving some kind of perfomance improvement when tested. Searching for a proper explanation to which it can be attributed to. @rejthy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.