Wednesday, March 30, 2016

Adding Missing Data points to Collections in Java

If you are from the .NET world, there are high chances that you have used LINQ in the .NET world. I was working on some JAVA project where there was some data which had missing datapoints.

This data in question was read from a DB, and was a big stored procedure. Making any more changes to it for filling in missing values was out of question, as it required nested loops which is better dealt in your programming language as it makes the program clean and more readable, plus its up the reader/consumer how he wants to prune/treat the data.
So here is a small snippet of code for filling missing data in a collection.


List<Timestamp> completeSeries= Utils.generateTimestampSequence(startTimestamp, endTimestamp);
completeSeries.stream().forEach((w) -> {
boolean found = interestingDataCollectionWithMissingPoints.stream().
anyMatch(x-> (x.getTimestamp().equals(w) ));
if(!found){
interestingDataCollectionWithMissingPoints.add(new InterestingDataClass(w, 0, 0, 0));
}
} );
//Now the interestingDataCollectionWithMissingPoints contains all the missing data points
Here, I had a certain data collection which was missing some data for a per minute time intervals. And I needed the complete data for all the point in times for the given time period (startTimeStamp and endTimeStamp).
So the little code snippet above does the trick. Java had no such feature prior to 1.5 version. Of course move to newer Java compiler version  to use this nifty feature.

<build>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <configuration>
     <source>1.8</source>
     <target>1.8</target>
    </configuration>
   </plugin>
                 </plugins>
</build>

No comments:

Interview Question Preperation : Find longest subarray whose sum is equal to K

Software Engineering Practice interview question Given a array of N elements. Find the length of Longest Subarray whose sum is equal to give...