Skip to content

Commit 5990347

Browse files
committed
Stream Exercises
1 parent 8b421fd commit 5990347

19 files changed

+649
-0
lines changed

src/StreamExercises/Exercise1.java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package StreamExercises;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
/**
7+
* Created by joris on 19.01.17.
8+
*/
9+
10+
/*
11+
Given is a list of the following Strings: "adam", "eva", "anna", "maria", "ruth".
12+
We want to print all names starting with ‘A’ in a sorted way. Use streams in combination with
13+
stream operations to solve the problem.
14+
Output is:
15+
ADAM
16+
ANNA
17+
*/
18+
public class Exercise1 {
19+
20+
public static void main(String[] args) {
21+
List<String> myList = Arrays.asList("adam", "eva", "anna", "maria", "ruth");
22+
myList.stream()
23+
.filter(a -> a.startsWith("a"))
24+
.map(String::toUpperCase)
25+
.sorted()
26+
.forEach(System.out::println);
27+
}
28+
}

src/StreamExercises/Exercise10.java

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package StreamExercises;
2+
3+
import java.util.stream.Stream;
4+
5+
/**
6+
* Created by joris on 19.01.17.
7+
*/
8+
public class Exercise10 {
9+
public static void main(String[] args) {
10+
Stream.of("Anna", "Adam", "Eva", "Tom", "Kurt")
11+
.map(s -> {
12+
System.out.println("map: " + s);
13+
return s.toUpperCase();
14+
})
15+
.filter(s -> {
16+
System.out.println("filter: " + s);
17+
return s.startsWith("A");
18+
})
19+
//prints foreach only for the filtered values
20+
.forEach(s -> System.out.println("forEach: " + s));
21+
}
22+
}
23+
24+
/*
25+
Output:
26+
map: Anna
27+
filter: ANNA
28+
forEach: ANNA
29+
map: Adam
30+
filter: ADAM
31+
forEach: ADAM
32+
map: Eva
33+
filter: EVA
34+
map: Tom
35+
filter: TOM
36+
map: Kurt
37+
filter: KURT
38+
39+
*/

src/StreamExercises/Exercise11.java

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package StreamExercises;
2+
3+
import java.util.stream.Stream;
4+
5+
/**
6+
* Created by joris on 19.01.17.
7+
*/
8+
public class Exercise11 {
9+
public static void main(String[] args) {
10+
Stream.of("Anna", "Adam", "Eva", "Tom", "Kurt")
11+
.filter(s -> {
12+
System.out.println("filter: " + s);
13+
return s.startsWith("A");
14+
})
15+
.sorted((s1, s2) -> {
16+
System.out.printf("sort: %s; %s\n", s1, s2);
17+
return s1.compareTo(s2);
18+
})
19+
.map(s -> {
20+
System.out.println("map: " + s);
21+
return s.toUpperCase();
22+
})
23+
.forEach(s -> System.out.println("forEach: " + s));
24+
}
25+
}
26+
27+
/*
28+
Output:
29+
filter: Anna
30+
filter: Adam
31+
filter: Eva
32+
filter: Tom
33+
filter: Kurt
34+
sort: Adam; Anna
35+
map: Adam
36+
forEach: ADAM
37+
map: Anna
38+
forEach: ANNA
39+
*/

src/StreamExercises/Exercise12.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package StreamExercises;
2+
3+
import java.util.function.Supplier;
4+
import java.util.stream.Stream;
5+
6+
/**
7+
* Created by joris on 19.01.17.
8+
*/
9+
public class Exercise12 {
10+
public static void main(String[] args) {
11+
Supplier<Stream<String>> streamSupplier = () -> Stream.of("Anna", "Adam", "Eva", "Tom", "Kurt")
12+
.filter(s -> s.startsWith("A"));
13+
System.out.println(streamSupplier.get().anyMatch(s -> true));
14+
System.out.println(streamSupplier.get().noneMatch(s -> true));
15+
}
16+
}
17+
18+
/*
19+
true
20+
false
21+
*/

src/StreamExercises/Exercise13.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package StreamExercises;
2+
3+
import java.util.stream.Stream;
4+
5+
/**
6+
* Created by joris on 19.01.17.
7+
*/
8+
public class Exercise13 {
9+
public static void main(String[] args) {
10+
Stream<String> stream = Stream.of("Anna", "Adam", "Eva", "Tom", "Kurt")
11+
.filter(s -> s.startsWith("A"));
12+
System.out.println(stream.anyMatch(s -> true));
13+
System.out.println(stream.noneMatch(s -> true));
14+
}
15+
}
16+
/*
17+
Output:
18+
Exception in thread "main" java.lang.IllegalStateException: stream has already been operated upon or closed
19+
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:229)
20+
at java.util.stream.ReferencePipeline.noneMatch(ReferencePipeline.java:459)
21+
*/

src/StreamExercises/Exercise14.java

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package StreamExercises;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
7+
/**
8+
* Created by joris on 19.01.17.
9+
*/
10+
/*
11+
Define a simple Person class.
12+
Then create a list of Persons with:
13+
List<Person> persons = Arrays.asList(
14+
new Person("Adam", 18),
15+
new Person("Peter", 23),
16+
new Person("Eva", 23),
17+
new Person("Moriz", 62),
18+
new Person("Max", 42));
19+
Use streams and functions to create a new filtered list which contains only persons which name start with M. Then display the new list.
20+
Output is:
21+
[Moriz, Max]
22+
*/
23+
public class Exercise14 {
24+
25+
26+
public static void main(String[] args) {
27+
class Person{
28+
String name;
29+
int age;
30+
31+
public Person(String name,int age){
32+
this.name = name;
33+
this.age = age;
34+
}
35+
public String toString(){return name;}
36+
}
37+
38+
List<Person> persons = Arrays.asList(new Person("Adam", 18), new Person("Peter", 23), new Person("Eva", 23), new Person("Moriz", 62), new Person("Max", 42));
39+
40+
List<Person> filteredpersons = persons.stream()
41+
.filter(p -> p.name.startsWith("M"))
42+
.collect(Collectors.toList());
43+
System.out.println(filteredpersons.toString());
44+
45+
System.out.println(persons.stream().map(p -> p.age).reduce((a,b) -> a+b));
46+
47+
}
48+
}

src/StreamExercises/Exercise15.java

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package StreamExercises;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
7+
/**
8+
* Created by joris on 19.01.17.
9+
*/
10+
/*
11+
We have the same situation as in the above exercise. Use streams and functions to calculate the
12+
average age of all persons. The average shall be calculated and displayed as a double.
13+
Output is:
14+
33.6
15+
Solution:
16+
*/
17+
public class Exercise15 {
18+
public static void main(String[] args) {
19+
class Person{
20+
String name;
21+
int age;
22+
23+
public Person(String name,int age){
24+
this.name = name;
25+
this.age = age;
26+
}
27+
public String toString(){return name;}
28+
}
29+
30+
List<Person> persons = Arrays.asList(new Person("Adam", 18), new Person("Peter", 23), new Person("Eva", 23), new Person("Moriz", 62), new Person("Max", 42));
31+
32+
persons.stream().mapToDouble(p -> p.age)
33+
.average()
34+
.ifPresent(System.out::println);
35+
36+
//alternate Solution
37+
Double averageAge = persons
38+
.stream()
39+
.collect(Collectors.averagingInt(p -> p.age));
40+
System.out.println(averageAge);
41+
}
42+
}

src/StreamExercises/Exercise16.java

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package StreamExercises;
2+
3+
import java.util.Arrays;
4+
import java.util.IntSummaryStatistics;
5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
8+
/**
9+
* Created by joris on 19.01.17.
10+
*/
11+
/*
12+
We have the same situation as in the above exercise.
13+
Use streams and functions to calculate and display the following:
14+
 Number of elements.
15+
 The sum of all ages.
16+
 The minimum age.
17+
 The average age.
18+
 The maximum age.
19+
Hint: Use IntSummaryStatistics and Collectors.summarizingInt.
20+
Output is:
21+
IntSummaryStatistics{count=5, sum=168, min=18, average=33.600000, max=62}
22+
*/
23+
public class Exercise16 {
24+
25+
public static void main(String[] args) {
26+
27+
28+
class Person {
29+
String name;
30+
int age;
31+
32+
public Person(String name, int age) {
33+
this.name = name;
34+
this.age = age;
35+
}
36+
37+
public String toString() {
38+
return name;
39+
}
40+
}
41+
42+
List<Person> persons = Arrays.asList(new Person("Adam", 18), new Person("Peter", 23), new Person("Eva", 23), new Person("Moriz", 62), new Person("Max", 42));
43+
44+
IntSummaryStatistics agesummary = persons.stream()
45+
.collect(Collectors.summarizingInt(p -> p.age));
46+
System.out.println(agesummary);
47+
}
48+
49+
}

src/StreamExercises/Exercise17.java

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package StreamExercises;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
7+
/**
8+
* Created by joris on 19.01.17.
9+
*/
10+
/*
11+
We have the same situation as in the above before.
12+
This time we want to create a String which contains the names of all persons which are of legal
13+
age (>=18) in Switzerland.
14+
When we print this String it shall show something like: In Switzerland Adam and Peter
15+
and Eva and Moriz and Max are of legal age.
16+
Again: Use streams and functions to create the String.
17+
Hint: Use Collectors.joining to create the final result.
18+
Output is:
19+
In Switzerland Adam and Peter and Eva and Moriz and Max are of legal age.
20+
*/
21+
public class Exercise17 {
22+
public static void main(String[] args) {
23+
class Person {
24+
String name;
25+
int age;
26+
27+
public Person(String name, int age) {
28+
this.name = name;
29+
this.age = age;
30+
}
31+
32+
public String toString() {
33+
return name;
34+
}
35+
}
36+
37+
List<Person> persons = Arrays.asList(new Person("Adam", 18), new Person("Peter", 23), new Person("Eva", 23), new Person("Moriz", 62), new Person("Max", 42));
38+
39+
String phrase = persons.stream()
40+
.filter(p -> p.age >= 18)
41+
.map(p -> p.name)
42+
.collect(Collectors.joining(" and ", "In Switzerland ", " are of legal age"));
43+
System.out.println(phrase);
44+
45+
}
46+
}
47+
48+
/*
49+
String phrase = persons.stream()
50+
.filter(p -> p.age >= 18)
51+
.map(p -> p.name)
52+
.collect(Collectors.joining(delimiter"and", prfix"In Switzerland", suffix"are of legal age"));
53+
*/

src/StreamExercises/Exercise18.java

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package StreamExercises;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.Map;
6+
import java.util.stream.Collectors;
7+
8+
/**
9+
* Created by joris on 19.01.17.
10+
*/
11+
/*
12+
We have the same situation as in the above before.
13+
We want to create a Map this time. The map shall contain as key the age off the persons and as
14+
value a string which contains the person of that age as you see below:
15+
{18=Adam, 23=Peter;Eva, 42=Max, 62=Moriz}
16+
Peter and Eva have the same age!
17+
Again: Use streams and functions to create the Map.
18+
Hint: Use Collectors.toMap to create the final result.
19+
Output is:
20+
{18=Adam, 23=Peter;Eva, 42=Max, 62=Moriz}
21+
*/
22+
public class Exercise18 {
23+
public static void main(String[] args) {
24+
class Person {
25+
String name;
26+
int age;
27+
28+
public Person(String name, int age) {
29+
this.name = name;
30+
this.age = age;
31+
}
32+
33+
public String toString() {
34+
return name;
35+
}
36+
}
37+
38+
List<Person> persons = Arrays.asList(new Person("Adam", 18), new Person("Peter", 23), new Person("Eva", 23), new Person("Moriz", 62), new Person("Max", 42));
39+
40+
Map<Integer,String> personmap = persons.stream()
41+
//third paramater merges, when to items with same key
42+
.collect(Collectors.toMap(p -> p.age,p -> p.name,(name1,name2) -> name1 + ";" + name2));
43+
System.out.println(personmap);
44+
}
45+
}

0 commit comments

Comments
 (0)