Skip to content

Commit

Permalink
feat: Java 8 flatMap example.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielso2007 committed Dec 21, 2019
1 parent da24e19 commit 1d2e83a
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package br.com.somejava8examples.commons.entities;

import java.util.HashSet;
import java.util.Set;

public class Student {

private String name;
private Set<String> book;

public Student() {
}

public Student(String name, Set<String> book) {
this.name = name;
this.book = book;
}

public void addBook(String book) {
if (this.book == null) {
this.book = new HashSet<>();
}
this.book.add(book);
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Set<String> getBook() {
return book;
}

public void setBook(Set<String> book) {
this.book = book;
}

@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", book=" + book +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package br.com.somejava8examples.map;

import br.com.somejava8examples.commons.entities.Student;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

@Service
public class FlatMapExampleService {

public Stream<String> streamStringFlatMap() {
String[][] data = new String[][]{{"a", "b"}, {"c", "d"}, {"e", "f"}};

//Stream<String[]>
Stream<String[]> temp = Arrays.stream(data);

//filter a stream of string[], and return a string[]?
Stream<String[]> stream = temp.filter(x -> "a".equals(x.toString()));

stream.forEach(System.out::println);

//Stream<String[]>
Stream<String[]> temp2 = Arrays.stream(data);

//Stream<String>, GOOD!
Stream<String> stringStream = temp.flatMap(x -> Arrays.stream(x));

Stream<String> stream2 = stringStream.filter(x -> "a".equals(x.toString()));

stream2.forEach(System.out::println);

return stream2;
}

public List<String> streamSetFlatMap() {
Student obj1 = new Student();
obj1.setName("mkyong");
obj1.addBook("Java 8 in Action");
obj1.addBook("Spring Boot in Action");
obj1.addBook("Effective Java (2nd Edition)");

Student obj2 = new Student();
obj2.setName("zilap");
obj2.addBook("Learning Python, 5th Edition");
obj2.addBook("Effective Java (2nd Edition)");

List<Student> list = new ArrayList<>();
list.add(obj1);
list.add(obj2);

List<String> collect =
list.stream()
.map(x -> x.getBook()) //Stream<Set<String>>
.flatMap(x -> x.stream()) //Stream<String>
.distinct()
.collect(Collectors.toList());

collect.forEach(x -> System.out.println(x));

return collect;
}

public IntStream streamPrimitiveFlatMapToInt() {
int[] intArray = {1, 2, 3, 4, 5, 6};

//1. Stream<int[]>
Stream<int[]> streamArray = Stream.of(intArray);

//2. Stream<int[]> -> flatMap -> IntStream
IntStream intStream = streamArray.flatMapToInt(x -> Arrays.stream(x));

intStream.forEach(x -> System.out.println(x));

return intStream;
}

}
31 changes: 31 additions & 0 deletions src/test/java/br/com/somejava8examples/map/FlatMapExampleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package br.com.somejava8examples.map;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

@SpringBootTest
@ActiveProfiles("test")
public class FlatMapExampleTest {

@Autowired
private FlatMapExampleService service;

@Test
public void sortByKeysTest() {
Assertions.assertNotNull(service.streamStringFlatMap());
}

@Test
public void streamSetFlatMapTest() {
Assertions.assertNotNull(service.streamSetFlatMap());
}

@Test
public void streamPrimitiveFlatMapToIntTest() {
Assertions.assertNotNull(service.streamPrimitiveFlatMapToInt());
}

}

0 comments on commit 1d2e83a

Please sign in to comment.