Employee
class for the custom object example.1. Filtering an Integer Array
Let’s start by filtering an array of integers. We’ll filter out numbers less than or equal to 5 and keep only the numbers greater than 5.
Example
import java.util.Arrays;
public class IntegerArrayFilter {
public static void main(String[] args) {
// Define the input array of integers
int[] intArray = {1, 6, 3, 9, 2, 7};
// Use Java 8 Stream to filter numbers greater than 5
int[] filteredArray = Arrays.stream(intArray)
.filter(num -> num > 5) // Filter numbers greater than 5
.toArray(); // Convert back to an array
// Display the filtered array
System.out.println("Filtered Integer Array (numbers > 5): " + Arrays.toString(filteredArray));
}
}
Output
Filtered Integer Array (numbers > 5): [6, 9, 7]
Explanation
Arrays.stream(intArray)
creates a stream from the integer array.- The
filter()
method is used to filter numbers greater than 5. toArray()
collects the filtered elements back into an array, which is then printed usingArrays.toString()
.
2. Filtering a String Array
Next, let’s filter a string array. In this example, we’ll filter strings that start with the letter "J".
Example
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class StringArrayFilter {
public static void main(String[] args) {
// Define the input array of strings
String[] stringArray = {"Java", "Python", "JavaScript", "C++", "Ruby"};
// Use Java 8 Stream to filter strings that start with "J"
List<String> filteredStrings = Arrays.stream(stringArray)
.filter(str -> str.startsWith("J")) // Filter strings starting with 'J'
.collect(Collectors.toList()); // Collect the result into a list
// Display the filtered list of strings
System.out.println("Filtered String Array (starts with 'J'): " + filteredStrings);
}
}
Output
Filtered String Array (starts with 'J'): [Java, JavaScript]
Explanation
Arrays.stream(stringArray)
converts the string array into a stream.- The
filter()
method checks if the strings start with the letter "J". collect(Collectors.toList())
collects the filtered elements into a list, which is printed.
3. Filtering a Custom Object Array (Using Employee
Class)
Lastly, let’s filter an array of Employee
objects based on their salary. We will filter employees who earn more than 50,000.
Example
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class EmployeeArrayFilter {
public static void main(String[] args) {
// Define the input array of Employee objects
Employee[] employeeArray = {
new Employee("John", 60000),
new Employee("Alice", 45000),
new Employee("Bob", 70000),
new Employee("Jane", 48000)
};
// Use Java 8 Stream to filter Employee objects based on salary
List<Employee> filteredEmployees = Arrays.stream(employeeArray)
.filter(employee -> employee.getSalary() > 50000) // Filter employees with salary > 50000
.collect(Collectors.toList()); // Collect the result into a list
// Display the filtered list of Employee objects
System.out.println("Filtered Employees (salary > 50000): " + filteredEmployees);
}
}
// Employee class definition
class Employee {
private String name;
private int salary;
public Employee(String name, int salary) {
this.name = name;
this.salary = salary;
}
public int getSalary() {
return salary;
}
@Override
public String toString() {
return name + " (" + salary + ")";
}
}
Output
Filtered Employees (salary > 50000): [John (60000), Bob (70000)]
Explanation
Arrays.stream(employeeArray)
converts the employee array into a stream.- The
filter()
method checks for employees with a salary greater than 50,000. collect(Collectors.toList())
collects the filtered employees into a list.- The filtered list is printed using the
toString()
method of theEmployee
class.
Conclusion
In Java 8, filtering arrays of different types—integers, strings, and custom objects like Employee
—is made simple with the Stream API. By using the filter()
method, you can easily specify conditions and process elements based on your requirements. Java Streams provide a clean and efficient way to handle filtering for various data types.
Comments
Post a Comment
Leave Comment