Start learning free

LESSON 268 OF 671 · RECORDS, SORTING & BUILT-INS

Positive Squares in Java: filter, then map

Practice Read the full explanation

You get an ArrayList of integers, nums.

Return a new ArrayList with the square of every number above 0. A number's square is the number times itself.

Keep the squares in the order of their numbers in nums.

Leave out 0 and the negative numbers. If no number is above 0, return an empty ArrayList.

 

Example 1:

Input: nums = [3, -1, 0, 4]
Output: [9, 16]
Explanation: Only 3 and 4 are above 0.
3 times 3 is 9, and 4 times 4 is 16.

Example 2:

Input: nums = [-5, -2, 0]
Output: []
Explanation: No number is above 0.

Example 3:

Input: nums = [1, 2, 3]
Output: [1, 4, 9]
Explanation: Every number is above 0, so every number is squared.

 

Constraints:

  • nums may be empty.
  • Every number is between -1000 and 1000.
STEP 1 OF 2
    Ctrl + Enter to check

    JAVA · RECORDS, SORTING & BUILT-INS

    Positive Squares in Java, explained

    Problem overview

    The method positiveSquares gets a list of integers and returns a new list with the square of every number above 0, in the order of the original numbers. Zero and negative numbers are left out, and if nothing is above 0 the result is an empty list.

    For [3, -1, 0, 4] the result is [9, 16]. For [-5, -2, 0] it is []. For [1, 2, 3] it is [1, 4, 9]. The input may be empty, and then the result is empty too.

    Approach

    Two things happen to the numbers: some are dropped, and the rest are changed. In a stream, filter drops and map changes, and the whole job fits in one chain: nums.stream().filter(...).map(...).toList().

    The order of the two steps matters. Filter first, so the test looks at the original numbers.

    Step by step

    1. Build a List<Integer> named squares in one line. Call stream() on nums.
    2. filter with num -> num > 0 keeps the numbers above 0. The test is strict, so 0 is left out.
    3. map with num -> num * num squares each of those numbers.
    4. End with toList(), then return squares.

    The solution in Java

    class Solution {
        public List<Integer> positiveSquares(List<Integer> nums) {
            List<Integer> squares = nums.stream().filter(num -> num > 0).map(num -> num * num).toList();
            return squares;
        }
    }
    

    The steps run in the order they are written. The stream keeps the original order of the numbers, so the squares come out in that order too. When no number passes the filter, toList() returns an empty list, which is the right answer and needs no special case.

    Edge cases and common mistakes

    • Squaring first. With .map(...).filter(num -> num > 0), the filter sees squares, which are never negative. For [3, -1, 0, 4] that version returns [9, 1, 16]: the -1 became 1 and passed, which is wrong. Zero is dropped only because 0 squared is still 0.
    • Using >= 0. That would keep 0 and add a 0 to the result.
    • Empty input or nothing above 0. The result is an empty list, printed as [].
    • Overflow. The numbers are between -1000 and 1000, so a square is at most 1,000,000, well inside the int range.
    • Changing the input. The stream builds a new list. nums is left as it was.

    Testing

    Try these inputs and results:

    • [3, -1, 0, 4] gives [9, 16].
    • [-5, -2, 0] gives [].
    • [1, 2, 3] gives [1, 4, 9].
    • An empty list gives [].

    To see why order matters, follow -1 through both versions. In the correct one the filter checks -1, which is not above 0, so it is dropped before it can be squared. In the wrong one it is squared first, becomes 1, and then passes a test that was meant for the original number.

    When a chain gives a surprising answer, test the stream one step at a time: print the list after filter alone, then add map. It shows at which step the unwanted number appears.

    The lambda parameter name is yours to choose. The solution calls it num in both lambdas, and each lambda has its own num: the one in map receives what the filter let through, not the original list item. Naming them differently, for example n and positive, is allowed and can make a long chain easier to read.

    Keep going with Java500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free