Junior — Middle
Have you used window functions for data processing in your projects?
sobes.tech AI
Answer from AI
Yes, in Java projects I used window functions, especially when working with data streams and collections. Window functions allow performing operations like moving averages, summing, or ranking without the need to group data, while maintaining the context of each row.
An example of using a window function with Java Stream API (via a library like jOOQ or custom code) might look like:
// Example: calculating moving average over a list of numbers
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
List<Double> movingAverages = new ArrayList<>();
int windowSize = 3;
for (int i = 0; i <= numbers.size() - windowSize; i++) {
double avg = numbers.subList(i, i + windowSize).stream()
.mapToInt(Integer::intValue)
.average()
.orElse(0);
movingAverages.add(avg);
}
System.out.println(movingAverages); // [2.0, 3.0, 4.0]
In SQL, which is often used with Java, window functions (e.g., ROW_NUMBER(), RANK(), SUM() OVER()) are widely used for analytics and reporting.