Continuo i test sugli Stream
This commit is contained in:
2
.idea/compiler.xml
generated
2
.idea/compiler.xml
generated
@@ -8,10 +8,12 @@
|
|||||||
<outputRelativeToContentRoot value="true" />
|
<outputRelativeToContentRoot value="true" />
|
||||||
<module name="modernjava" />
|
<module name="modernjava" />
|
||||||
<module name="scala" />
|
<module name="scala" />
|
||||||
|
<module name="modern-java" />
|
||||||
</profile>
|
</profile>
|
||||||
</annotationProcessing>
|
</annotationProcessing>
|
||||||
<bytecodeTargetLevel>
|
<bytecodeTargetLevel>
|
||||||
<module name="fpgym" target="8" />
|
<module name="fpgym" target="8" />
|
||||||
|
<module name="modern-java" target="9" />
|
||||||
<module name="modernjava" target="9" />
|
<module name="modernjava" target="9" />
|
||||||
<module name="scala" target="1.5" />
|
<module name="scala" target="1.5" />
|
||||||
</bytecodeTargetLevel>
|
</bytecodeTargetLevel>
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
package org.gym.fp.moderjava;
|
package org.gym.fp.moderjava;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.*;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import static java.lang.System.out;
|
import static java.lang.System.out;
|
||||||
|
|
||||||
@@ -12,6 +11,8 @@ public class StreamTest {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
doStreamFilterDemo();
|
doStreamFilterDemo();
|
||||||
doStreamMappingDemo();
|
doStreamMappingDemo();
|
||||||
|
doStreamFindOrMatchingDemo();
|
||||||
|
doStreamReducingDemo();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void doStreamFilterDemo() {
|
private static void doStreamFilterDemo() {
|
||||||
@@ -48,6 +49,7 @@ public class StreamTest {
|
|||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
out.println(uniqueCharacters);
|
out.println(uniqueCharacters);
|
||||||
out.println("----------------------------------------");
|
out.println("----------------------------------------");
|
||||||
|
out.println("Permutations");
|
||||||
List<Integer> numbers1 = Arrays.asList(1, 2, 3);
|
List<Integer> numbers1 = Arrays.asList(1, 2, 3);
|
||||||
List<Integer> numbers2 = Arrays.asList(3, 4);
|
List<Integer> numbers2 = Arrays.asList(3, 4);
|
||||||
List<int[]> pairs1 = numbers1.stream()
|
List<int[]> pairs1 = numbers1.stream()
|
||||||
@@ -60,6 +62,91 @@ public class StreamTest {
|
|||||||
.filter(pair -> (pair[0] + pair[1]) % 3 == 0)
|
.filter(pair -> (pair[0] + pair[1]) % 3 == 0)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
pairs2.forEach(pair -> out.println(Arrays.toString(pair)));
|
pairs2.forEach(pair -> out.println(Arrays.toString(pair)));
|
||||||
|
out.println("----------------------------------------");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void doStreamFindOrMatchingDemo() {
|
||||||
|
out.println("ANYMATCH EXAMPLE");
|
||||||
|
if (getDishes().stream().anyMatch(Dish::isVegetarian)) {
|
||||||
|
out.println("The menu is (somewhat) vegetarian friendly!!!");
|
||||||
|
}
|
||||||
|
out.println("----------------------------------------");
|
||||||
|
out.println("ALLMATCH EXAMPLE");
|
||||||
|
if (getDishes().stream().allMatch(dish -> dish.getCalories() < 1000)) {
|
||||||
|
out.println("This menù is healthy");
|
||||||
|
}
|
||||||
|
out.println("----------------------------------------");
|
||||||
|
out.println("NONEMATCH EXAMPLE");
|
||||||
|
if (getDishes().stream().noneMatch(dish -> dish.getCalories() >= 1000)) {
|
||||||
|
out.println("This menù is healthy");
|
||||||
|
}
|
||||||
|
out.println("----------------------------------------");
|
||||||
|
out.println("FINDANY EXAMPLE");
|
||||||
|
Optional<Dish> dish = getDishes().stream()
|
||||||
|
.filter(Dish::isVegetarian)
|
||||||
|
.findAny();
|
||||||
|
dish.ifPresent(out::println);
|
||||||
|
out.println("----------------------------------------");
|
||||||
|
out.println("FINDFIRST EXAMPLE");
|
||||||
|
List<Integer> someNumbers = Arrays.asList(1, 2, 3, 4, 5);
|
||||||
|
someNumbers.stream()
|
||||||
|
.map(n -> n * n)
|
||||||
|
.filter(n -> n % 3 == 0)
|
||||||
|
.findFirst()
|
||||||
|
.ifPresent(out::println);
|
||||||
|
out.println("----------------------------------------");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void doStreamReducingDemo() {
|
||||||
|
out.println("REDUCING");
|
||||||
|
int sum = Stream.of(4, 5, 3, 9).reduce(0, Integer::sum);
|
||||||
|
out.println(sum);
|
||||||
|
out.println("----------------------------------------");
|
||||||
|
out.println("MAX & MIN");
|
||||||
|
Stream.of(4, 5, 3, 9).reduce(Integer::max).ifPresent(out::println);
|
||||||
|
Stream.of(4, 5, 3, 9).reduce(Integer::min).ifPresent(out::println);
|
||||||
|
out.println("----------------------------------------");
|
||||||
|
out.println("COUNT DISHES");
|
||||||
|
getDishes().stream()
|
||||||
|
.map(d -> 1)
|
||||||
|
.reduce(Integer::sum)
|
||||||
|
.ifPresent(out::println);
|
||||||
|
out.println("----------------------------------------");
|
||||||
|
out.println("PUTTING ALL IN PRACTICE");
|
||||||
|
Trader raoul = new Trader("Raoul", "Cambridge");
|
||||||
|
Trader mario = new Trader("Mario", "Milan");
|
||||||
|
Trader alan = new Trader("Alan", "Cambridge");
|
||||||
|
Trader brian = new Trader("Brian", "Cambridge");
|
||||||
|
List<Transaction> transactions = Arrays.asList(
|
||||||
|
new Transaction(brian, 2011, 300),
|
||||||
|
new Transaction(raoul, 2012, 1000),
|
||||||
|
new Transaction(raoul, 2011, 400),
|
||||||
|
new Transaction(mario, 2012, 710),
|
||||||
|
new Transaction(mario, 2012, 700),
|
||||||
|
new Transaction(alan, 2012, 950)
|
||||||
|
);
|
||||||
|
out.println("1)");
|
||||||
|
transactions.stream()
|
||||||
|
.filter(t -> t.getYear() == 2011)
|
||||||
|
.sorted(Comparator.comparing(Transaction::getValue))
|
||||||
|
.collect(Collectors.toList())
|
||||||
|
.forEach(out::println);
|
||||||
|
out.println("2)");
|
||||||
|
transactions.stream()
|
||||||
|
.map(Transaction::getTrader)
|
||||||
|
.map(Trader::getCity)
|
||||||
|
.distinct()
|
||||||
|
.collect(Collectors.toList())
|
||||||
|
.forEach(out::println);
|
||||||
|
out.println("3)");
|
||||||
|
transactions.stream()
|
||||||
|
.map(Transaction::getTrader)
|
||||||
|
.filter(trader -> "Cambridge".equalsIgnoreCase(trader.getCity()))
|
||||||
|
.sorted(Comparator.comparing(Trader::getName))
|
||||||
|
.collect(Collectors.toList())
|
||||||
|
.forEach(out::println);
|
||||||
|
|
||||||
|
out.println("----------------------------------------");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<Dish> getDishes() {
|
private static List<Dish> getDishes() {
|
||||||
@@ -71,7 +158,7 @@ public class StreamTest {
|
|||||||
new Dish("tomato", 30, Dish.Type.VEGETARIAN),
|
new Dish("tomato", 30, Dish.Type.VEGETARIAN),
|
||||||
new Dish("tunny", 120, Dish.Type.FISH),
|
new Dish("tunny", 120, Dish.Type.FISH),
|
||||||
new Dish("potato", 70, Dish.Type.VEGETARIAN)
|
new Dish("potato", 70, Dish.Type.VEGETARIAN)
|
||||||
);
|
);
|
||||||
return Collections.unmodifiableList(dishes);
|
return Collections.unmodifiableList(dishes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
11
modern-java/src/main/java/org/gym/fp/moderjava/Trader.java
Normal file
11
modern-java/src/main/java/org/gym/fp/moderjava/Trader.java
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package org.gym.fp.moderjava;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Value;
|
||||||
|
|
||||||
|
@Value
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class Trader {
|
||||||
|
private final String name;
|
||||||
|
private final String city;
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package org.gym.fp.moderjava;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Value;
|
||||||
|
|
||||||
|
@Value
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class Transaction {
|
||||||
|
private final Trader trader;
|
||||||
|
private final int year;
|
||||||
|
private final int value;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user