eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Partner – LambdaTest – NPI EA (cat=Testing)
announcement - icon

Regression testing is an important step in the release process, to ensure that new code doesn't break the existing functionality. As the codebase evolves, we want to run these tests frequently to help catch any issues early on.

The best way to ensure these tests run frequently on an automated basis is, of course, to include them in the CI/CD pipeline. This way, the regression tests will execute automatically whenever we commit code to the repository.

In this tutorial, we'll see how to create regression tests using Selenium, and then include them in our pipeline using GitHub Actions:, to be run on the LambdaTest cloud grid:

>> How to Run Selenium Regression Tests With GitHub Actions

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

1. Overview

Moving averages are a fundamental tool in analyzing trends and patterns in data and are widely used in finance, economics, and engineering.

They help smooth out short-term fluctuations and reveal underlying trends, making data easier to interpret.

In this tutorial, we’ll explore various methods and techniques for calculating moving averages, ranging from traditional approaches to libraries and Stream API’s.

2. Common Methods for Calculating Moving Averages

In this section, we’ll explore three common methods for calculating moving averages.

2.1. Using Apache Commons Math Library

Apache Commons Math is a powerful Java library that offers a wide range of mathematical and statistical functions, including tools for calculating moving averages.

By utilizing the DescriptiveStatistics class from the Apache Commons Math library, we can streamline the process of moving average computation and leverage optimized algorithms for efficient data processing. It adds data points to the statistics object and retrieves the mean, which represents the moving average.

Let’s use the DescriptiveStatistics class to calculate a moving average with a windowSize:

public class MovingAverageWithApacheCommonsMath {

    private final DescriptiveStatistics stats;

    public MovingAverageWithApacheCommonsMath(int windowSize) {
        this.stats = new DescriptiveStatistics(windowSize);
    }

    public void add(double value) {
        stats.addValue(value);
    }

    public double getMovingAverage() {
        return stats.getMean();
    }
}

Let’s test our implementation:

@Test
public void whenValuesAreAdded_shouldUpdateAverageCorrectly() {
    MovingAverageWithApacheCommonsMath movingAverageCalculator = new MovingAverageWithApacheCommonsMath(3);
    movingAverageCalculator.add(10);
    assertEquals(10.0, movingAverageCalculator.getMovingAverage(), 0.001);
    movingAverageCalculator.add(20);
    assertEquals(15.0, movingAverageCalculator.getMovingAverage(), 0.001);
    movingAverageCalculator.add(30);
    assertEquals(20.0, movingAverageCalculator.getMovingAverage(), 0.001);
}

First, we create an instance of the MovingAverageWithApacheCommonsMath class with a window size of 3. Then, three values (10, 20, and 30) are added to the calculator individually, and its mean is verified.

2.2. Using Circular Buffer Approach

The circular buffer approach is a classic method for calculating moving averages and is known for its efficient memory usage. This approach is straightforward and may offer better performance in some cases, especially if we’re concerned about the overhead of external dependencies.

In this approach, new data points overwrite the oldest ones, and the average is calculated based on the current elements in the buffer.

By cycling through the buffer circularly, we can achieve a constant time complexity for each update, making it suitable for real-time data processing applications.

Let’s calculate the moving average using a circular buffer:

public class MovingAverageByCircularBuffer {

    private final double[] buffer;
    private int head;
    private int count;

    public MovingAverageByCircularBuffer(int windowSize) {
        this.buffer = new double[windowSize];
    }

    public void add(double value) {
        buffer[head] = value;
        head = (head + 1) % buffer.length;
        if (count < buffer.length) {
            count++;
        }
    }

    public double getMovingAverage() {
        if (count == 0) {
            return Double.NaN;
        }
        double sum = 0;
        for (int i = 0; i < count; i++) {
            sum += buffer[i];
        }
        return sum / count;
    }
}

Let’s write a test case to verify the method:

@Test
public void whenValuesAreAdded_shouldUpdateAverageCorrectly() {
    MovingAverageByCircularBuffer ma = new MovingAverageByCircularBuffer(3);
    ma.add(10);
    assertEquals(10.0, ma.getMovingAverage(), 0.001);
    ma.add(20);
    assertEquals(15.0, ma.getMovingAverage(), 0.001);
    ma.add(30);
    assertEquals(20.0, ma.getMovingAverage(), 0.001);
}

We create an instance of the MovingAverageByCircularBuffer class with a window size of 3. After adding each value, the test asserts that the calculated moving average matches the expected value with a tolerance of 0.001.

2.3. Using Exponential Moving Average

Another approach is to use exponential smoothing to calculate the moving average.

Exponential smoothing assigns exponentially decreasing weights to older observations, which can be useful for capturing trends and reacting quickly to changes in the data:

public class ExponentialMovingAverage {

    private double alpha;
    private Double previousEMA;

    public ExponentialMovingAverage(double alpha) {
        if (alpha <= 0 || alpha > 1) {
            throw new IllegalArgumentException("Alpha must be in the range (0, 1]");
        }
        this.alpha = alpha;
        this.previousEMA = null;
    }

    public double calculateEMA(double newValue) {
        if (previousEMA == null) {
            previousEMA = newValue;
        } else {
            previousEMA = alpha * newValue + (1 - alpha) * previousEMA;
        }
        return previousEMA;
    }
}

Here, the alpha parameter controls the rate of decay, with smaller values giving more weight to recent observations.

Exponential moving averages are particularly useful when we want to react quickly to changes in the data while still capturing longer-term trends.

Let’s verify it with a test case:

@Test
public void whenValuesAreAdded_shouldUpdateExponentialMovingAverageCorrectly() {
    ExponentialMovingAverage ema = new ExponentialMovingAverage(0.4);
    assertEquals(10.0, ema.calculateEMA(10.0), 0.001);
    assertEquals(14.0, ema.calculateEMA(20.0), 0.001);
    assertEquals(20.4, ema.calculateEMA(30.0), 0.001);
}

We first create an instance of ExponentialMovingAverage (EMA) with a smoothing factor (alpha) of 0.4.

Then, as each value is added, the test asserts that the calculated EMA matches the expected value within a small tolerance of 0.001.

2.4. Stream-Based Approach

We can leverage the Stream API to calculate moving averages in a more functional and declarative manner. This approach is particularly useful if we want to deal with data streams or collections.

Here’s a simplified example of how we can use a stream-based approach to calculate a moving average:

public class MovingAverageWithStreamBasedApproach {
    private int windowSize;

    public MovingAverageWithStreamBasedApproach(int windowSize) {
        this.windowSize = windowSize;
    }
    public double calculateAverage(double[] data) {
        return DoubleStream.of(data)
                .skip(Math.max(0, data.length - windowSize))
                .limit(Math.min(data.length, windowSize))
                .summaryStatistics()
                .getAverage();
    }
}

Here , we created a stream from the input data array, skipping elements outside the specified window size, limiting the stream to the windowSize, and then using summaryStatistics() to calculate the average.

This approach leverages the functional programming capabilities of Java Streams API to perform the calculation in a concise and efficient manner.

Now, let’s write some JUnit tests to make sure our code works as expected:

@Test
public void whenValidDataIsPassed_shouldReturnCorrectAverage() {
    double[] data = {10, 20, 30, 40, 50};
    int windowSize = 3;
    double expectedAverage = 40;
    MovingAverageWithStreamBasedApproach calculator = new MovingAverageWithStreamBasedApproach(windowSize);
    double actualAverage = calculator.calculateAverage(data);
    assertEquals(expectedAverage, actualAverage);
}

In these tests, we check if our calculateAverage() method returns the correct average for given scenarios such as valid data and windowSize.

3. Additional Methods

While the above methods are some of the more convenient and efficient ways to calculate moving averages in Java, there are alternative approaches we can consider depending on our specific requirements and constraints. Here, we’ll introduce two such approaches.

3.1. Parallel Processing

If performance is our top priority and we have access to multiple CPU cores, we can utilize parallel processing techniques to compute moving averages more efficiently.

Java provides support for parallel streams, which can automatically distribute computation across multiple threads.

3.2. Weighted Moving Average

The Weighted Moving Average (WMA) is a method for calculating moving averages that assigns different weights to each data point within the window.

The weights are typically determined based on predefined criteria such as significance, relevance, or proximity to the center of the window.

3.3. Cumulative Moving Average

The Cumulative Moving Average (CMA) calculates the average of all data points up to a certain point in time. Unlike other moving average methods, CMA does not use a fixed-size window but includes all available data.

4. Conclusion

Calculating moving averages is a fundamental aspect of time-series analysis, with applications spanning various domains such as finance, economics, and engineering.

Using Apache Commons Math, circular buffer, and exponential moving average techniques, analysts can gain valuable insights into their data’s underlying trends and patterns.

Moreover, exploring weighted and cumulative moving averages expands the analyst’s toolkit, enabling more sophisticated analysis and interpretation of time-series data.

Again, the choice depends entirely on specific project requirements and preferences.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)