Skip to content

Commit bacde4a

Browse files
committed
Numeric streams, building streams, infinite streams
1 parent 1a38020 commit bacde4a

File tree

7 files changed

+146
-5
lines changed

7 files changed

+146
-5
lines changed

src/main/java/lambdasinaction/chap3/ExecuteAround.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static void main(String ...args) throws IOException{
1313
}
1414

1515
public static String processFile(BufferedReaderProcessor p) throws IOException {
16-
try(BufferedReader br = new BufferedReader(new FileReader("src/main/java/lambdasinaction/chap3/data.txt"))){
16+
try(BufferedReader br = new BufferedReader(new FileReader("lambdasinaction/chap3/data.txt"))){
1717
return p.process(br);
1818
}
1919

@@ -23,4 +23,4 @@ public interface BufferedReaderProcessor{
2323
public String process(BufferedReader b) throws IOException;
2424

2525
}
26-
}
26+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package lambdasinaction.chap4;
2+
3+
import java.util.*;
4+
import java.util.function.IntSupplier;
5+
import java.util.stream.*;
6+
import static java.util.stream.Collectors.toList;
7+
import java.nio.charset.Charset;
8+
import java.nio.file.*;
9+
10+
public class BuildingStreams {
11+
12+
public static void main(String...args) throws Exception{
13+
14+
// Stream.of
15+
Stream<String> stream = Stream.of("Java 8", "Lambdas", "In", "Action");
16+
stream.map(String::toUpperCase).forEach(System.out::println);
17+
18+
// Stream.empty
19+
Stream<String> emptyStream = Stream.empty();
20+
21+
// Arrays.stream
22+
int[] numbers = {2, 3, 5, 7, 11, 13};
23+
System.out.println(Arrays.stream(numbers).sum());
24+
25+
// Stream.iterate
26+
Stream.iterate(0, n -> n + 2)
27+
.limit(10)
28+
.forEach(System.out::println);
29+
30+
// fibonnaci with iterate
31+
Stream.iterate(new int[]{0, 1}, t -> new int[]{t[1],t[0] + t[1]})
32+
.limit(10)
33+
.forEach(t -> System.out.println("(" + t[0] + ", " + t[1] + ")"));
34+
35+
Stream.iterate(new int[]{0, 1}, t -> new int[]{t[1],t[0] + t[1]})
36+
.limit(10)
37+
. map(t -> t[0])
38+
.forEach(System.out::println);
39+
40+
// random stream of doubles with Stream.generate
41+
Stream.generate(Math::random)
42+
.limit(10)
43+
.forEach(System.out::println);
44+
45+
// stream of 1s with Stream.generate
46+
IntStream.generate(() -> 1)
47+
.limit(5)
48+
.forEach(System.out::println);
49+
50+
IntStream.generate(new IntSupplier(){
51+
public int getAsInt(){
52+
return 2;
53+
}
54+
}).limit(5)
55+
.forEach(System.out::println);
56+
57+
58+
IntSupplier fib = new IntSupplier(){
59+
private int previous = 0;
60+
private int current = 1;
61+
public int getAsInt(){
62+
int nextValue = this.previous + this.current;
63+
this.previous = this.current;
64+
this.current = nextValue;
65+
return this.previous;
66+
}
67+
};
68+
IntStream.generate(fib).limit(10).forEach(System.out::println);
69+
70+
long uniqueWords = Files.lines(Paths.get("lambdasinaction/chap4/data.txt"), Charset.defaultCharset())
71+
.flatMap(line -> Arrays.stream(line.split(" ")))
72+
.distinct()
73+
.count();
74+
75+
System.out.println("There are " + uniqueWords + " unique words in data.txt");
76+
77+
78+
}
79+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package lambdasinaction.chap4;
2+
import java.util.stream.*;
3+
import java.util.*;
4+
5+
import static lambdasinaction.chap4.Dish.menu;
6+
7+
public class NumericStreams{
8+
9+
public static void main(String...args){
10+
11+
List<Integer> numbers = Arrays.asList(3,4,5,1,2);
12+
13+
Arrays.stream(numbers.toArray()).forEach(System.out::println);
14+
int calories = menu.stream()
15+
.mapToInt(Dish::getCalories)
16+
.sum();
17+
System.out.println("Number of calories:" + calories);
18+
19+
20+
// max and OptionalInt
21+
OptionalInt maxCalories = menu.stream()
22+
.mapToInt(Dish::getCalories)
23+
.max();
24+
25+
int max;
26+
if(maxCalories.isPresent()){
27+
max = maxCalories.getAsInt();
28+
}
29+
else {
30+
// we can choose a default value
31+
max = 1;
32+
}
33+
System.out.println(max);
34+
35+
// numeric ranges
36+
IntStream evenNumbers = IntStream.rangeClosed(1, 100)
37+
.filter(n -> n % 2 == 0);
38+
39+
System.out.println(evenNumbers.count());
40+
41+
Stream<int[]> pythagoreanTriples =
42+
IntStream.rangeClosed(1, 100).boxed()
43+
.flatMap(a -> IntStream.rangeClosed(a, 100)
44+
.filter(b -> Math.sqrt(a*a + b*b) % 1 == 0).boxed()
45+
.map(b -> new int[]{a, b, (int) Math.sqrt(a * a + b * b)}));
46+
47+
pythagoreanTriples.forEach(t -> System.out.println(t[0] + ", " + t[1] + ", " + t[2]));
48+
49+
}
50+
51+
public static boolean isPerfectSquare(int n){
52+
return Math.sqrt(n) % 1 == 0;
53+
}
54+
55+
}

src/main/java/lambdasinaction/chap4/Reducing.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,11 @@ public static void main(String...args){
2020

2121
Optional<Integer> min = numbers.stream().reduce(Integer::min);
2222
min.ifPresent(v -> System.out.println(v));
23+
24+
25+
int calories = menu.stream()
26+
.map(Dish::getCalories)
27+
.reduce(0, Integer::sum);
28+
System.out.println("Number of calories:" + calories);
2329
}
2430
}

src/main/java/lambdasinaction/chap4/StreamBasic.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
import java.util.stream.*;
55
import static java.util.stream.Collectors.toList;
66

7-
import lambdasinaction.chap5.Dish;
8-
import static lambdasinaction.chap5.Dish.menu;
7+
import static lambdasinaction.chap4.Dish.menu;
98

109
public class StreamBasic {
1110

@@ -17,4 +16,4 @@ public static void main(String...args){
1716
.collect(toList());
1817
System.out.println(names);
1918
}
20-
}
19+
}
File renamed without changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The quick brown fox jumped over the lazy dog
2+
The lazy dog jumped over the quick brown fox

0 commit comments

Comments
 (0)