Aggiunto modulo per esempi del libro "Modern Java in Action"
This commit is contained in:
45
modern-java/pom.xml
Normal file
45
modern-java/pom.xml
Normal file
@@ -0,0 +1,45 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>modern-java</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>9</source>
|
||||
<target>9</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<parent>
|
||||
<artifactId>fpgym</artifactId>
|
||||
<groupId>org.gym.fp</groupId>
|
||||
<version>1.0</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>9</maven.compiler.source>
|
||||
<maven.compiler.target>9</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
26
modern-java/src/main/java/org/gym/fp/moderjava/Dish.java
Normal file
26
modern-java/src/main/java/org/gym/fp/moderjava/Dish.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package org.gym.fp.moderjava;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Value;
|
||||
|
||||
@Value
|
||||
@AllArgsConstructor
|
||||
public final class Dish {
|
||||
private final String name;
|
||||
private final int calories;
|
||||
private final Type type;
|
||||
|
||||
public enum Type {
|
||||
MEAT, FISH, VEGETARIAN, OTHER
|
||||
}
|
||||
|
||||
public boolean isVegetarian() {
|
||||
return this.type == Type.VEGETARIAN;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package org.gym.fp.moderjava;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static java.lang.System.out;
|
||||
|
||||
public class StreamTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
doStreamFilterDemo();
|
||||
doStreamMappingDemo();
|
||||
}
|
||||
|
||||
private static void doStreamFilterDemo() {
|
||||
out.println("FILTER EXAMPLE");
|
||||
getDishes().stream()
|
||||
.filter(Dish::isVegetarian)
|
||||
.collect(Collectors.toList())
|
||||
.forEach(out::println);
|
||||
out.println("----------------------------------------");
|
||||
out.println("DISTINCT EXAMPLE");
|
||||
getDishes().stream()
|
||||
.filter(dish -> dish.getName().length() > 4)
|
||||
.distinct()
|
||||
.forEach(out::println);
|
||||
out.println("----------------------------------------");
|
||||
out.println("TAKEWHILE EXAMPLE");
|
||||
getDishes().stream()
|
||||
.takeWhile(dish -> dish.getCalories() > 40)
|
||||
.forEach(out::println);
|
||||
out.println("----------------------------------------");
|
||||
out.println("DROPWHILE EXAMPLE");
|
||||
getDishes().stream()
|
||||
.dropWhile(dish -> dish.getCalories() > 40)
|
||||
.forEach(out::println);
|
||||
out.println("----------------------------------------");
|
||||
}
|
||||
|
||||
private static void doStreamMappingDemo() {
|
||||
List<String> words = Arrays.asList("Hello", "World");
|
||||
List<String> uniqueCharacters = words.stream()
|
||||
.map(word -> word.split(""))
|
||||
.flatMap(Arrays::stream)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
out.println(uniqueCharacters);
|
||||
out.println("----------------------------------------");
|
||||
List<Integer> numbers1 = Arrays.asList(1, 2, 3);
|
||||
List<Integer> numbers2 = Arrays.asList(3, 4);
|
||||
List<int[]> pairs1 = numbers1.stream()
|
||||
.flatMap(i -> numbers2.stream().map(j -> new int[]{i, j}))
|
||||
.collect(Collectors.toList());
|
||||
pairs1.forEach(pair -> out.println(Arrays.toString(pair)));
|
||||
out.println("----------------------------------------");
|
||||
List<int[]> pairs2 = numbers1.stream()
|
||||
.flatMap(i -> numbers2.stream().map(j -> new int[]{i, j}))
|
||||
.filter(pair -> (pair[0] + pair[1]) % 3 == 0)
|
||||
.collect(Collectors.toList());
|
||||
pairs2.forEach(pair -> out.println(Arrays.toString(pair)));
|
||||
}
|
||||
|
||||
private static List<Dish> getDishes() {
|
||||
List<Dish> dishes = Arrays.asList(
|
||||
new Dish("pork", 300, Dish.Type.MEAT),
|
||||
new Dish("salad", 50, Dish.Type.VEGETARIAN),
|
||||
new Dish("chicken", 100, Dish.Type.MEAT),
|
||||
new Dish("chicken", 100, Dish.Type.MEAT),
|
||||
new Dish("tomato", 30, Dish.Type.VEGETARIAN),
|
||||
new Dish("tunny", 120, Dish.Type.FISH),
|
||||
new Dish("potato", 70, Dish.Type.VEGETARIAN)
|
||||
);
|
||||
return Collections.unmodifiableList(dishes);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user