BERN, Switzerland -- Canada and Sweden will meet in the semifinals for a fourth straight year at the ice hockey world championship after both won tight matches on Thursday.
Canada got past Latvia for a 4-2 win on Matthew Lombardi's goal early in the third period.
Stamkos' goal was a tournament-best seventh for the rookie forward who was picked first in the 2008 NHL draft.
"He has been in the right spot and scored timely goals for us," Canada coach Lindy Ruff said. "He came in with a lot of confidence and it's showing. He's been able to flourish."
Sweden finally put away the Czech Republic 3-1 on a goal from veteran captain Kenny Jonsson with 2:14 to play in a quarterfinal.
"I know we're going to play a tough opponent but we still haven't lost in regulation so far," Sweden forward Marcus Nilson said. "I think we've got a good chance."
Canada beat Sweden the past two years, winning it all in 2007, while Sweden won the final four matchup on its way to the world title in 2006, the year it also won Olympic gold in Turin.
The teams meet on Friday after the United States plays defending champion and tournament favorite Russia.
Canada also hit penalty trouble on Thursday, giving Latvia five power-play chances in the final period.
"If we are frustrated we are easier to play against," captain Shane Doan said.
Goalie Chris Mason had 21 saves for Canada. Edgars Masalskis made 44 for Latvia.
"They did a good job of disrupting where we wanted to go. We need to improve obviously," Ruff said.
Heatley gave Canada the lead with a one-timer off Derek Roy's pass from the left circle for his sixth goal of the tournament.
Hamhuis scored on a power play shot from just inside the blue line, then Latvia scored on a short-handed breakaway goal from Guntis Galvins.
The 19-year-old Stamkos restored the two-goal lead on the next shift, but the spirited Latvians responded early in the third through Herberts Vasiljevs, who slapped a shot past Mason from between the circles.
Canada pulled away for good when Lombardi skated between two defenders and scored.
The Czechs' frustration quickly led to two more penalties but instead of putting the game out of reach on their two-man advantage, the Swedes gave the puck away to Tomas Rolinek, who went down the ice and fed Petr Cajanek for a one-timer in the slot.
The Czechs came inches away from an equalizer when their next shot ricocheted from the crossbar and bounced a couple of times on the ice behind Gustavsson before the puck was cleared.
Gustavsson had 33 saves for the win, and Jakub Stepanek had 17 stops for the 2005 champion Czechs.
"We've got a great goaltender and when there was rebounds around our defensemen did a good job of clearing them out," Nilson said.