<s>[INST] class Solution {
    public int[] asteroidCollision(int[] asteroids) {
        Stack<Integer> stack = new Stack<>();
        
        for (int asteroid : asteroids) {
            if (stack.isEmpty() || asteroid > 0 || (stack.peek() < 0)) {
                stack.push(asteroid);
            }
            else {
                while (!stack.isEmpty() && stack.peek() > 0 && Math.abs(stack.peek()) < Math.abs(asteroid)) {
                    stack.pop();
                }
                if (!stack.isEmpty() && stack.peek() > 0) {
                    if (Math.abs(stack.peek()) == Math.abs(asteroid)) {
                        stack.pop();
                    }
                    continue;
                }
                stack.push(asteroid);
            }
        }
        int[] res = new int[stack.size()];
        int idx = stack.size() -1;
        while (!stack.isEmpty()) {
            res[idx--] = stack.pop();
        }
        return res;
    }
}

Why does the author of this code use a while loop to pop elements from the stack?[/INST]