🧠 Java Stream API Mastery – Exercise Set
📦 Given Sample Data
```java
List<String> names = Arrays.asList("Steve", "Amanda", "Paul", "Sam", "Jessica",
"Robert", "Alice", "Tom", "John", "Peter");
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<List<Integer>> matrix = Arrays.asList(Arrays.asList(1, 2), Arrays.asList(3,
4), Arrays.asList(5, 6));
```
✅ Level 1: Basics – Stream Creation & ForEach
1. Create a stream from `names` and print each name.
2. Print only even numbers from `numbers` using `.filter()`.
3. Print squares of each number using `.map()`.
🔄 Level 2: Intermediate Operations
4. From `names`, print names starting with "A".
5. Convert all names to uppercase and print them.
6. Sort the names in reverse alphabetical order.
7. Print the first 3 even numbers from `numbers`.
8. Skip the first 5 elements and print the rest from `numbers`.
9. Flatten `matrix` into a single list and print all values using `flatMap()`.
🎯 Level 3: Terminal Operations
10. Collect all odd numbers from `numbers` into a `List<Integer>`.
11. Count how many names have more than 4 characters.
12. Find the maximum number in `numbers`.
13. Reduce `numbers` to get the sum of all values.
14. Check if any name starts with "J" using `anyMatch()`.
15. Get the first element from the list using `findFirst()`.
🚀 Level 4: Advanced Practice
16. Group `names` by the first letter using `Collectors.groupingBy()`.
17. Partition `numbers` into even and odd using `Collectors.partitioningBy()`.
18. Join all names with commas into a single string using `Collectors.joining()`.
19. Convert `names` into a `Map` where key = name, value = length of name.
20. Find the name with the longest length using `stream().max()`.
🧪 Bonus Challenge (Mini Project)
**Input:** List of Employees
```java
class Employee {
String name;
String department;
double salary;
int age;
// constructor, getters
}
List<Employee> employees = Arrays.asList(
new Employee("Alice", "HR", 30000, 25),
new Employee("Bob", "IT", 50000, 30),
new Employee("Carol", "HR", 35000, 28),
new Employee("David", "IT", 60000, 35),
new Employee("Eve", "Sales", 40000, 29)
);
```
✍️ Tasks:
1. Print names of employees in IT department.
2. Get average salary of employees using `Collectors.averagingDouble()`.
3. Get list of employees with salary > 40000.
4. Group employees by department.
5. Find employee with highest salary.
6. Count employees per department.