Stream groupBy optimization and refactoring