Learn Java 8 streams with an example - print odd/even numbers from Array and List
Example 1: Java 8 program to print odd numbers from a List
import java.util.Arrays;import java.util.List;import java.util.stream.Collectors;
/*Java 8 Program to find Odd Numbers from a List*/public class DriverClass { public static void main(String[] args) { List<Integer> numbers = Arrays. asList(1, 4, 8, 40, 11, 22, 33, 99); List<Integer> oddNumbers = numbers.stream(). filter(o -> o % 2 != 0). collect(Collectors.toList()); System.out.println(oddNumbers); }}
import java.util.Arrays;import java.util.List;import java.util.stream.Collectors;
/*Java 8 Program to find Odd Numbers from a List*/public class DriverClass { public static void main(String[] args) { List<Integer> numbers = Arrays. asList(1, 4, 8, 40, 11, 22, 33, 99); List<Integer> oddNumbers = numbers.stream(). filter(o -> o % 2 != 0). collect(Collectors.toList()); System.out.println(oddNumbers); }}
Example 2: Java 8 program to print even numbers from a List
import java.util.Arrays;import java.util.List;import java.util.stream.Collectors;
/*Java 8 Program to find Even Numbers from a List*/public class DriverClass { public static void main(String[] args) { List<Integer> numbers = Arrays. asList(1, 4, 8, 40, 11, 22, 33, 99); List<Integer> evenNumbers = numbers.stream(). filter(o -> o % 2 == 0). collect(Collectors.toList()); System.out.println(evenNumbers); }}
import java.util.Arrays;import java.util.List;import java.util.stream.Collectors;
/*Java 8 Program to find Even Numbers from a List*/public class DriverClass { public static void main(String[] args) { List<Integer> numbers = Arrays. asList(1, 4, 8, 40, 11, 22, 33, 99); List<Integer> evenNumbers = numbers.stream(). filter(o -> o % 2 == 0). collect(Collectors.toList()); System.out.println(evenNumbers); }}
Example 3: Java 8 program to print even numbers from an array
import java.util.Arrays;
/*Java 8 Program to find Even Numbers from an Array*/public class DriverClass { public static void main(String[] args) { int[] numbers = {2, 5, 7, 8, 99, 1, 22, 4, 3, 77, 66}; Arrays.stream(numbers).filter(o -> o % 2 == 0). forEach(System.out::println); }}
import java.util.Arrays;
/*Java 8 Program to find Even Numbers from an Array*/public class DriverClass { public static void main(String[] args) { int[] numbers = {2, 5, 7, 8, 99, 1, 22, 4, 3, 77, 66}; Arrays.stream(numbers).filter(o -> o % 2 == 0). forEach(System.out::println); }}
Example 4: Java 8 program to print odd numbers from an array
import java.util.Arrays;
/*Java 8 Program to find Odd Numbers from an Array*/public class DriverClass { public static void main(String[] args) { int[] numbers = {2, 5, 7, 8, 99, 1, 22, 4, 3, 77, 66}; Arrays.stream(numbers).filter(o -> o % 2 != 0). forEach(System.out::println); }}
import java.util.Arrays;
/*Java 8 Program to find Odd Numbers from an Array*/public class DriverClass { public static void main(String[] args) { int[] numbers = {2, 5, 7, 8, 99, 1, 22, 4, 3, 77, 66}; Arrays.stream(numbers).filter(o -> o % 2 != 0). forEach(System.out::println); }}
Example 5: Java 8 program to print odd numbers from a Set
import java.util.Set;import java.util.stream.Collectors;
/*Java 8 Program to find Odd Numbers from a set*/public class DriverClass { public static void main(String[] args) { Set<Integer> numbers = Set.of(1, 4, 8, 40, 11, 22, 33, 99); Set<Integer> oddNumbers = numbers.stream(). filter(o -> o % 2 != 0). collect(Collectors.toSet()); System.out.println(oddNumbers); }}
import java.util.Set;import java.util.stream.Collectors;
/*Java 8 Program to find Odd Numbers from a set*/public class DriverClass { public static void main(String[] args) { Set<Integer> numbers = Set.of(1, 4, 8, 40, 11, 22, 33, 99); Set<Integer> oddNumbers = numbers.stream(). filter(o -> o % 2 != 0). collect(Collectors.toSet()); System.out.println(oddNumbers); }}
1. Created Immutable Set using Set.of() method2. Java Set interface provides stream() method which returns a sequential Stream with set of Integer as its source.3. The Java stream filter() method allows us to narrow down the stream elements based on a criterion. Here we separated the odd numbers from all other numbers.4. Using collect() method we put all odd numbers to a Set.Example 6: Java 8 program to print even numbers from a Set
import java.util.Set;import java.util.stream.Collectors;/*Java 8 Program to find even numbers from a set*/public class DriverClass {public static void main(String[] args) {Set<Integer> numbers =Set.of(1, 4, 8, 40, 11, 22, 33, 99);Set<Integer> evenNumbers = numbers.stream().filter(o -> o % 2 == 0).collect(Collectors.toSet());System.out.println(evenNumbers);}}
1. Created Immutable Set using Set.of() method
2. Java Set interface provides stream() method which returns a sequential Stream with set of Integer as its source.
3. The Java stream filter() method allows us to narrow down the stream elements based on a criterion. Here we separated the even numbers from all other numbers.
4. Using collect() method we put all even numbers to a Set.
Unlock the power of modern Java and transform your coding skills with
Modern Java in Action: Lambdas, Streams, Functional and Reactive Programming (2nd Edition)! 🌟
📚 Master the latest Java features with expert insights from
Raoul-Gabriel Urma, Alan Mycroft, and Mario Fusco. 🚀
💡 Elevate your programming game with Lambdas, Streams, Functional and Reactive programming techniques.
🔥 Unlock cleaner, more efficient code and embrace the future of Java!
🌐 Whether you're a seasoned developer or a curious learner, this book is your ultimate guide to mastering modern Java.
💥 Buy Now & Save 15%! 💥
Your Java journey begins here! ✨
More interesting topics,
Unlock the power of modern Java and transform your coding skills with
Modern Java in Action: Lambdas, Streams, Functional and Reactive Programming (2nd Edition)! 🌟
📚 Master the latest Java features with expert insights from
Raoul-Gabriel Urma, Alan Mycroft, and Mario Fusco. 🚀
💡 Elevate your programming game with Lambdas, Streams, Functional and Reactive programming techniques.
🔥 Unlock cleaner, more efficient code and embrace the future of Java!
🌐 Whether you're a seasoned developer or a curious learner, this book is your ultimate guide to mastering modern Java.
💥 Buy Now & Save 15%! 💥
Your Java journey begins here! ✨