TOKYO — Japan’s economy grew at an annual rate of 5.9 percent in the first quarter, the fastest rate in nearly three years, as companies and consumers spent to beat a sales tax increase that is expected to cause a contraction in the current quarter.

The growth figures for Japan, released Thursday, were in line with expectations. Strong private consumption, residential investment and corporate spending were the biggest contributors to growth, while public spending fell slightly.

Japan’s quarter-to-quarter growth in January through March, adjusted for inflation, was 1.5 percent, the fastest rate since the rebound from the March 2011 earthquake and tsunami. It was the sixth straight quarter of expansion.

Since taking office in late 2012, Prime Minister Shinzo Abe has sought to get Japan’s growth back on track with heavy government spending, low interest rates and economic overhauls.