020-005-003

Collection Operations: Sorting and Filtering Lists

Hard

Problem Description

In this problem, you will create a program that adds elements to an integer ArrayList, sorts them in ascending order, filters to keep only even numbers, and displays the result to standard output.

collection Operations

ArrayList supports various operations like sorting and filtering.

Key Methods

ArrayList<Integer> list = new ArrayList<>();
list.add(5);  // Add element
Collections.sort(list);  // Sort ascending
list.removeIf(n -> n % 2 != 0);  // Filter by condition

Learning Points

  • Use Collections.sort() to sort lists
  • Use removeIf() to remove elements not matching condition
  • Use isEmpty() to check if list is empty

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?

Sign Up