Skip to content

Commit d28ed31

Browse files
committed
Add classes for stream operations: ConcatTwoStreams, FlatMapExample, Person, and SumProductMaxUsingStreams
1 parent 3c05cfe commit d28ed31

4 files changed

Lines changed: 75 additions & 0 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.java8;
2+
3+
import java.util.stream.Stream;
4+
5+
public class ConcatTwoStreams {
6+
public static void main(String[] args) {
7+
Stream<String> stream1 = Stream.of("Java", "Python");
8+
Stream<String> stream2 = Stream.of("C", ".NET");
9+
Stream<String> concatStream = Stream.concat(stream1, stream2);
10+
concatStream.forEach(System.out::println);
11+
12+
}
13+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.java8;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public class FlatMapExample {
7+
public static void main(String[] args) {
8+
List<Person> personList = Arrays.asList(new Person("Alice", Arrays.asList("Red", "Yellow")),
9+
new Person("Tom", Arrays.asList("Blue", "Orange", "Red")),
10+
new Person("Bob", Arrays.asList("Red", "Yellow", "Black")));
11+
List<List<String>> colorList = personList.stream().map(Person::getColor).toList();
12+
System.out.println(colorList);
13+
List<String> colors = personList.stream().flatMap(x->x.getColor().stream()).toList();
14+
System.out.println(colors);
15+
}
16+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.java8;
2+
3+
import java.util.List;
4+
5+
public class Person {
6+
String name;
7+
List<String> color;
8+
9+
public Person(String name, List<String> color) {
10+
this.name = name;
11+
this.color = color;
12+
}
13+
14+
public String getName() {
15+
return name;
16+
}
17+
18+
public void setName(String name) {
19+
this.name = name;
20+
}
21+
22+
public List<String> getColor() {
23+
return color;
24+
}
25+
26+
public void setColor(List<String> color) {
27+
this.color = color;
28+
}
29+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.java8;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.Optional;
6+
7+
public class SumProductMaxUsingStreams {
8+
public static void main(String[] args) {
9+
List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);
10+
Optional<Integer> sum = integerList.stream().reduce((a, b) -> a + b);
11+
System.out.println("Sum of given list of integers: " + sum.orElse(1));
12+
Optional<Integer> max = integerList.stream().reduce(Integer::max);
13+
System.out.println("Maximum of given list of integers: " + max.orElse(-1));
14+
Optional<Integer> product = integerList.stream().reduce((a, b) -> a * b);
15+
System.out.println("Product of given list of integers: " + product.orElse(1));
16+
}
17+
}

0 commit comments

Comments
 (0)