java loop optimization