Created
July 11, 2020 06:32
-
-
Save javabuddy/27062748ca2352c3620205c3649b4efd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tool; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
/** | |
* | |
* A simple Java Program to demonstrate how to use map and filter method Java 8. | |
* In this program, we'll convert a list of String into a list of Integer and | |
* then filter all even numbers. | |
*/ | |
public class Hello { | |
public static void main(String[] args) { | |
List<String> numbers = Arrays.asList("1", "2", "3", "4", "5", "6"); | |
System.out.println("original list: " + numbers); | |
List<Integer> even = numbers.stream() | |
.map(s -> Integer.valueOf(s)) | |
.filter(number -> number % 2 == 0) | |
.collect(Collectors.toList()); | |
System.out.println("processed list, only even numbers: " + even); | |
} | |
} | |
Output | |
original list: [1, 2, 3, 4, 5, 6] | |
the processed list, only even numbers: [2, 4, 6] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment