Created
January 30, 2019 17:48
-
-
Save brpaz/d80de96e7d20ba2aae95dc74fb6cfed0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 com.mkyong.java8; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
public class TestJava8 { | |
public static void main(String[] args) { | |
List<String> alpha = Arrays.asList("a", "b", "c", "d"); | |
//Before Java8 | |
List<String> alphaUpper = new ArrayList<>(); | |
for (String s : alpha) { | |
alphaUpper.add(s.toUpperCase()); | |
} | |
System.out.println(alpha); //[a, b, c, d] | |
System.out.println(alphaUpper); //[A, B, C, D] | |
// Java 8 | |
List<String> collect = alpha.stream().map(String::toUpperCase).collect(Collectors.toList()); | |
System.out.println(collect); //[A, B, C, D] | |
// Extra, streams apply to any data type. | |
List<Integer> num = Arrays.asList(1,2,3,4,5); | |
List<Integer> collect1 = num.stream().map(n -> n * 2).collect(Collectors.toList()); | |
System.out.println(collect1); //[2, 4, 6, 8, 10] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment