map vs flatMap
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package com.javadevjournal;
|
||||
|
||||
/**
|
||||
* Hello world!
|
||||
*
|
||||
*/
|
||||
public class App
|
||||
{
|
||||
public static void main( String[] args )
|
||||
{
|
||||
System.out.println( "Hello World!" );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.javadevjournal;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class FlatMapExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
List<String> words= Arrays.asList("JavaDevJournal","Java");
|
||||
List<String> unique= words.stream()
|
||||
.map( s-> s.split(""))
|
||||
.flatMap(Arrays::stream)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
unique.forEach(System.out::println);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.javadevjournal;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class MapExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
List<String> words= Arrays.asList("Java Dev Journal","Java", "Spring Boot","Java 8");
|
||||
|
||||
List<Integer> wordCount = words.stream()
|
||||
.map(String::length)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
wordCount.forEach(System.out::println);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.javadevjournal;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
public class AppTest
|
||||
{
|
||||
/**
|
||||
* Rigorous Test :-)
|
||||
*/
|
||||
@Test
|
||||
public void shouldAnswerWithTrue()
|
||||
{
|
||||
assertTrue( true );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user