simplify some loops in core util and stream