Type-1:
Below is a Java program that takes an ArrayList of integers as input and returns a new ArrayList containing only the even numbers from the original list:
import java.util.ArrayList;
import java.util.List;
public class EvenNumbers {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(15);
numbers.add(6);
numbers.add(27);
numbers.add(8);
List<Integer> evenNumbers = getEvenNumbers(numbers);
System.out.println("Original list: " + numbers);
System.out.println("Even numbers: " + evenNumbers);
}
public static List<Integer> getEvenNumbers(List<Integer> numbers) {
List<Integer> evenNumbers = new ArrayList<>();
for (int num : numbers) {
if (num % 2 == 0) {
evenNumbers.add(num);
}
}
return evenNumbers;
}
}
Explanation:
- We define a class called EvenNumbers.
- In the main method, we create an ArrayList called numbers containing some integers. You can modify this list with your desired input integers.
- We call the getEvenNumbers method to extract the even numbers from the numbers list.
- The getEvenNumbers method takes an ArrayList of integers as input and returns a new ArrayList called evenNumbers, which will contain only the even numbers from the original list.
- The method iterates through each element in the input numbers list. For each number, it checks if it's even by using the modulo operator % 2. If the result is 0, it means the number is even, so it adds it to the evenNumbers list.
- Finally, the main method prints the original list and the list containing only the even numbers.
You can change the input list in the main method to test the program with different sets of integers.
Type-2:
Another way to filter even numbers from the original list is by using Java Streams.
Java Streams provide a concise and expressive way to manipulate collections.
Here's the modified Java program using Streams:
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class EvenNumbers {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(15);
numbers.add(6);
numbers.add(27);
numbers.add(8);
List<Integer> evenNumbers = getEvenNumbers(numbers);
System.out.println("Original list: " + numbers);
System.out.println("Even numbers: " + evenNumbers);
}
public static List<Integer> getEvenNumbers(List<Integer> numbers) {
return numbers.stream()
.filter(num -> num % 2 == 0)
.collect(Collectors.toList());
}
}
Explanation:
- In this version, the getEvenNumbers method uses Java Streams to filter the even numbers from the input numbers list.
- The stream() method converts the list into a Stream of elements.
- The filter method takes a predicate (a lambda expression) that tests each element of the stream. In this case, it checks if the number is even (num % 2 == 0).
- The collect method collects the filtered elements into a new list using the Collectors.toList() method.
The output will be the same as the previous version, but this approach provides a more compact and expressive way of filtering even numbers from the original list.
0 Comments