forked from pr2-lecture/uebungen
Update of exercises
parent
0c8383fd67
commit
7811de699d
|
|
@ -1,6 +1,6 @@
|
|||
# Kleinbuchstaben in einem String zählen
|
||||
## Lernziel
|
||||
[Musterlösung](solution/)
|
||||
|
||||
|
||||
|
||||
Streams verwenden.
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
# Buchstaben in einer Liste von Strings zählen
|
||||
## Lernziel
|
||||
[Musterlösung](solution/)
|
||||
|
||||
|
||||
|
||||
Streams verwenden.
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
# Kleinbuchstaben in einem String zählen
|
||||
## Lernziel
|
||||
[Musterlösung](solution/)
|
||||
|
||||
|
||||
|
||||
Streams verwenden.
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
# Liste filtern
|
||||
## Lernziel
|
||||
[Musterlösung](solution/)
|
||||
|
||||
|
||||
|
||||
Daten in Streams filtern.
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
# Streams mit interner Iteration
|
||||
## Lernziel
|
||||
[Musterlösung](solution/)
|
||||
|
||||
|
||||
|
||||
Einen Stream mit einer internen Iteration verwenden. Die Iteration sowohl mit einem Lambda, als auch mit einer Methodenreferenz durchführen.
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
# Map und Filter auf Streams
|
||||
## Lernziel
|
||||
[Musterlösung](solution/)
|
||||
|
||||
|
||||
|
||||
Einen Stream durch Map- und Filter-Operationen analysieren und gewünschte Daten extrahieren.
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
# Map und Reduce auf Streams
|
||||
## Lernziel
|
||||
[Musterlösung](solution/)
|
||||
|
||||
|
||||
|
||||
Die Daten in einem Stream durch Map- und Reduce verdichten.
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
# Reduce
|
||||
## Lernziel
|
||||
[Musterlösung](solution/)
|
||||
|
||||
|
||||
|
||||
Daten in Streams mit `reduce` aggregieren.
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
# Summe über reduce bestimmen
|
||||
## Lernziel
|
||||
[Musterlösung](solution/)
|
||||
|
||||
|
||||
|
||||
Daten in Streams mit `reduce` aggregieren.
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
# Wörter zählen
|
||||
## Lernziel
|
||||
[Musterlösung](solution/)
|
||||
|
||||
|
||||
|
||||
Daten in Streams mit `reduce` aggregieren.
|
||||
|
|
|
|||
20
readme.md
20
readme.md
|
|
@ -101,16 +101,16 @@ Hinweise zur nötigen Softwareausstattung finden Sie [hier](help/softwareausstat
|
|||
| 87. | Collections | [CommandLineSorter -- Version 2](Collections_009/readme.md) | [✅](Collections_009/solution/) |
|
||||
| 88. | Collections | [`Comparator<T>` verwenden und Objekte sortieren](Collections_010/readme.md) | [✅](Collections_010/solution/) |
|
||||
| 89. | Collections | [Wörterbuchprogramm](Collections_011/readme.md) | [✅](Collections_011/solution/) |
|
||||
| 90. | Streams | [Kleinbuchstaben in einem String zählen](Streams_001/readme.md) | [✅](Streams_001/solution/) |
|
||||
| 91. | Streams | [Buchstaben in einer Liste von Strings zählen](Streams_002/readme.md) | [✅](Streams_002/solution/) |
|
||||
| 92. | Streams | [Kleinbuchstaben in einem String zählen](Streams_003/readme.md) | [✅](Streams_003/solution/) |
|
||||
| 93. | Streams | [Liste filtern](Streams_004/readme.md) | [✅](Streams_004/solution/) |
|
||||
| 94. | Streams | [Streams mit interner Iteration](Streams_005/readme.md) | [✅](Streams_005/solution/) |
|
||||
| 95. | Streams | [Map und Filter auf Streams](Streams_006/readme.md) | [✅](Streams_006/solution/) |
|
||||
| 96. | Streams | [Map und Reduce auf Streams](Streams_007/readme.md) | [✅](Streams_007/solution/) |
|
||||
| 97. | Streams | [Reduce](Streams_008/readme.md) | [✅](Streams_008/solution/) |
|
||||
| 98. | Streams | [Summe über reduce bestimmen](Streams_009/readme.md) | [✅](Streams_009/solution/) |
|
||||
| 99. | Streams | [Wörter zählen](Streams_010/readme.md) | [✅](Streams_010/solution/) |
|
||||
| 90. | Streams | [Kleinbuchstaben in einem String zählen](Streams_001/readme.md) | |
|
||||
| 91. | Streams | [Buchstaben in einer Liste von Strings zählen](Streams_002/readme.md) | |
|
||||
| 92. | Streams | [Kleinbuchstaben in einem String zählen](Streams_003/readme.md) | |
|
||||
| 93. | Streams | [Liste filtern](Streams_004/readme.md) | |
|
||||
| 94. | Streams | [Streams mit interner Iteration](Streams_005/readme.md) | |
|
||||
| 95. | Streams | [Map und Filter auf Streams](Streams_006/readme.md) | |
|
||||
| 96. | Streams | [Map und Reduce auf Streams](Streams_007/readme.md) | |
|
||||
| 97. | Streams | [Reduce](Streams_008/readme.md) | |
|
||||
| 98. | Streams | [Summe über reduce bestimmen](Streams_009/readme.md) | |
|
||||
| 99. | Streams | [Wörter zählen](Streams_010/readme.md) | |
|
||||
| 100. | Threads | [Fakultät parallel berechnen](Threads_001/readme.md) | [✅](Threads_001/solution/) |
|
||||
| 101. | Threads | [Thread per interrupt beenden](Threads_002/readme.md) | [✅](Threads_002/solution/) |
|
||||
| 102. | Threads | [wait und notify benutzen](Threads_003/readme.md) | [✅](Threads_003/solution/) |
|
||||
|
|
|
|||
|
|
@ -1,13 +0,0 @@
|
|||
package pr2.streams.chars_1;
|
||||
|
||||
public class CountLetters {
|
||||
public static long count(String s) {
|
||||
long sum;
|
||||
|
||||
sum = s.chars()
|
||||
.filter(Character::isLowerCase)
|
||||
.count();
|
||||
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
package pr2.streams.chars_2;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class CountLetters {
|
||||
public static long count(List<String> liste) {
|
||||
|
||||
long chars;
|
||||
|
||||
chars = liste.stream()
|
||||
.flatMapToInt(s -> s.chars())
|
||||
.count();
|
||||
|
||||
return chars;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
package pr2.streams.chars_3;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class CountLetters {
|
||||
public static long count(List<String> liste) {
|
||||
|
||||
long chars;
|
||||
|
||||
chars = liste.stream()
|
||||
.flatMapToInt(s -> s.chars())
|
||||
.filter(Character::isLowerCase)
|
||||
.count();
|
||||
|
||||
return chars;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
package pr2.streams.filter;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class FilterList {
|
||||
public static List<String> filter(List<String> liste) {
|
||||
List<String> result;
|
||||
|
||||
result = liste.stream()
|
||||
.filter(s -> s.length() >= 5)
|
||||
.map(String::toLowerCase)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,87 +0,0 @@
|
|||
package pr2.streams.interne_iteration;
|
||||
|
||||
/**
|
||||
* Computerspiel.
|
||||
*/
|
||||
public class ComputerGame {
|
||||
|
||||
/**
|
||||
* Name.
|
||||
*/
|
||||
private final String name;
|
||||
|
||||
/**
|
||||
* Jahr.
|
||||
*/
|
||||
private final int year;
|
||||
|
||||
/**
|
||||
* Metacritic.
|
||||
*/
|
||||
private final int metaScore;
|
||||
|
||||
/**
|
||||
* Userscore.
|
||||
*/
|
||||
private final int userScore;
|
||||
|
||||
/**
|
||||
* Legt ein neues Spiel an.
|
||||
*
|
||||
* @param name Name des Spiels.
|
||||
* @param year Jahr.
|
||||
* @param metaScore Metacritic-Bewertung.
|
||||
* @param userScore Nutzer-Bewertung.
|
||||
*/
|
||||
public ComputerGame(String name, int year, int metaScore, int userScore) {
|
||||
this.name = name;
|
||||
this.year = year;
|
||||
this.metaScore = metaScore;
|
||||
this.userScore = userScore;
|
||||
}
|
||||
|
||||
/**
|
||||
* Name des Spiels.
|
||||
*
|
||||
* @return Der Name
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Jahr, in dem das Spiel erschienen ist bzw. kritisiert wurde.
|
||||
*
|
||||
* @return Das Erscheinungsjahr.
|
||||
*/
|
||||
public int getYear() {
|
||||
return year;
|
||||
}
|
||||
|
||||
/**
|
||||
* Die Metacritic-Bewertung (0-100).
|
||||
*
|
||||
* @return Die Bewertung.
|
||||
*/
|
||||
public int getMetaScore() {
|
||||
return metaScore;
|
||||
}
|
||||
|
||||
/**
|
||||
* Die Benutzerbewertung (0-100).
|
||||
*
|
||||
* @return Die Benutzerbewertung.
|
||||
*/
|
||||
public int getUserScore() {
|
||||
return userScore;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%d;%s;%s;%d", metaScore, name, userScore / 10.0,
|
||||
year);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,61 +0,0 @@
|
|||
package pr2.streams.interne_iteration;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Datenbank für die Spiele.
|
||||
*/
|
||||
public class GameDatabase {
|
||||
|
||||
private final List<ComputerGame> games;
|
||||
|
||||
/**
|
||||
* Datenbank mit Spieleinfomrationen.
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public GameDatabase() {
|
||||
games = new ArrayList<>();
|
||||
|
||||
try {
|
||||
BufferedReader br =
|
||||
new BufferedReader(new FileReader(
|
||||
"games.txt"));
|
||||
|
||||
String line;
|
||||
|
||||
while ((line = br.readLine()) != null) {
|
||||
String[] tokens = line.split(";");
|
||||
int metacritic = Integer.parseInt(tokens[0]);
|
||||
String name = tokens[1];
|
||||
int userScore = (int) (Double.parseDouble(tokens[2]) * 10);
|
||||
Date date =
|
||||
new SimpleDateFormat("MMM d, yyyy", Locale.US).parse(
|
||||
tokens[3]);
|
||||
|
||||
games.add(new ComputerGame(name, date.getYear() + 1900,
|
||||
metacritic, userScore));
|
||||
}
|
||||
|
||||
br.close();
|
||||
} catch (IOException | ParseException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt die Liste der Spiele zurück.
|
||||
*
|
||||
* @return Liste der Spiele
|
||||
*/
|
||||
public List<ComputerGame> getGames() {
|
||||
return new ArrayList<>(games);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,39 +0,0 @@
|
|||
package pr2.streams.interne_iteration;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* Spiele auflisten.
|
||||
*/
|
||||
public class ListGames {
|
||||
|
||||
/**
|
||||
* Hauptmethode.
|
||||
*
|
||||
* @param args Kommandozeilenargumente.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
GameDatabase db = new GameDatabase();
|
||||
|
||||
System.out.println("Interne Iterartion");
|
||||
System.out.println();
|
||||
Stream<ComputerGame> stream = db.getGames().stream();
|
||||
stream.forEach(g -> System.out.println(g));
|
||||
|
||||
System.out.println();
|
||||
System.out.println();
|
||||
System.out.println("Interne Iterartion mit Methodenreferenz");
|
||||
stream = db.getGames().stream();
|
||||
stream.forEach(System.out::println);
|
||||
|
||||
System.out.println();
|
||||
System.out.println();
|
||||
System.out.println("Spiele aus 2012");
|
||||
stream = db.getGames().stream();
|
||||
stream.forEach(g -> {
|
||||
if (g.getYear() == 2012) {
|
||||
System.out.println(g);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -1,88 +0,0 @@
|
|||
package pr2.streams.map_filter;
|
||||
|
||||
/**
|
||||
* Computerspiel.
|
||||
*/
|
||||
public class ComputerGame {
|
||||
|
||||
/**
|
||||
* Name.
|
||||
*/
|
||||
private final String name;
|
||||
|
||||
/**
|
||||
* Jahr.
|
||||
*/
|
||||
private final int year;
|
||||
|
||||
/**
|
||||
* Metacritic.
|
||||
*/
|
||||
private final int metaScore;
|
||||
|
||||
/**
|
||||
* Userscore.
|
||||
*/
|
||||
private final int userScore;
|
||||
|
||||
/**
|
||||
* Legt ein neues Spiel an.
|
||||
*
|
||||
* @param name Name des Spiels.
|
||||
* @param year Jahr.
|
||||
* @param metaScore Metacritic-Bewertung.
|
||||
* @param userScore Nutzer-Bewertung.
|
||||
*/
|
||||
public ComputerGame(String name, int year, int metaScore, int userScore) {
|
||||
|
||||
this.name = name;
|
||||
this.year = year;
|
||||
this.metaScore = metaScore;
|
||||
this.userScore = userScore;
|
||||
}
|
||||
|
||||
/**
|
||||
* Name des Spiels.
|
||||
*
|
||||
* @return Der Name
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Jahr, in dem das Spiel erschienen ist bzw. kritisiert wurde.
|
||||
*
|
||||
* @return Das Erscheinungsjahr.
|
||||
*/
|
||||
public int getYear() {
|
||||
return year;
|
||||
}
|
||||
|
||||
/**
|
||||
* Die Metacritic-Bewertung (0-100).
|
||||
*
|
||||
* @return Die Bewertung.
|
||||
*/
|
||||
public int getMetaScore() {
|
||||
return metaScore;
|
||||
}
|
||||
|
||||
/**
|
||||
* Die Benutzerbewertung (0-100).
|
||||
*
|
||||
* @return Die Benutzerbewertung.
|
||||
*/
|
||||
public int getUserScore() {
|
||||
return userScore;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%d;%s;%s;%d", metaScore, name, userScore / 10.0,
|
||||
year);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,61 +0,0 @@
|
|||
package pr2.streams.map_filter;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Datenbank für die Spiele.
|
||||
*/
|
||||
public class GameDatabase {
|
||||
|
||||
private final List<ComputerGame> games;
|
||||
|
||||
/**
|
||||
* Datenbank mit Spieleinfomrationen.
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public GameDatabase() {
|
||||
games = new ArrayList<>();
|
||||
|
||||
try {
|
||||
BufferedReader br =
|
||||
new BufferedReader(new FileReader(
|
||||
"games.txt"));
|
||||
|
||||
String line;
|
||||
|
||||
while ((line = br.readLine()) != null) {
|
||||
String[] tokens = line.split(";");
|
||||
int metacritic = Integer.parseInt(tokens[0]);
|
||||
String name = tokens[1];
|
||||
int userScore = (int) (Double.parseDouble(tokens[2]) * 10);
|
||||
Date date =
|
||||
new SimpleDateFormat("MMM d, yyyy", Locale.US).parse(
|
||||
tokens[3]);
|
||||
|
||||
games.add(new ComputerGame(name, date.getYear() + 1900,
|
||||
metacritic, userScore));
|
||||
}
|
||||
|
||||
br.close();
|
||||
} catch (IOException | ParseException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt die Liste der Spiele zurück.
|
||||
*
|
||||
* @return Liste der Spiele
|
||||
*/
|
||||
public List<ComputerGame> getGames() {
|
||||
return new ArrayList<>(games);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,47 +0,0 @@
|
|||
package pr2.streams.map_filter;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* Spiele auflisten.
|
||||
*/
|
||||
public class MapFilterGames {
|
||||
|
||||
/**
|
||||
* Hauptmethode.
|
||||
*
|
||||
* @param args Kommandozeilenargumente.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
GameDatabase db = new GameDatabase();
|
||||
List<ComputerGame> games = db.getGames();
|
||||
|
||||
// Alle Spiele von 2010 mit einem Metascore >= 80
|
||||
// und einem User-Score >= 80
|
||||
Stream<ComputerGame> stream = games.stream();
|
||||
stream.filter(g -> g.getYear() == 2010)
|
||||
.filter(g -> g.getMetaScore() >= 80)
|
||||
.filter(g -> g.getUserScore() >= 80).forEach(
|
||||
System.out::println);
|
||||
|
||||
// Größter und kleinster Unterschied
|
||||
stream = games.stream();
|
||||
Optional<ComputerGame> o = stream.max((a, b) -> (
|
||||
a.getMetaScore() - a.getUserScore() - b.getMetaScore()
|
||||
+ b.getUserScore()));
|
||||
if (o.isPresent()) {
|
||||
System.out.println("Größte Abweichung: " + o.get());
|
||||
}
|
||||
|
||||
stream = games.stream();
|
||||
o = stream.min((a, b) -> (
|
||||
a.getMetaScore() - a.getUserScore() - b.getMetaScore()
|
||||
+ b.getUserScore()));
|
||||
|
||||
if (o.isPresent()) {
|
||||
System.out.println("Kleinste Abweichung: " + o.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
package pr2.streams.reduce;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static java.util.stream.Collectors.summingInt;
|
||||
|
||||
public class Squarer {
|
||||
public static int mitMap(List<Integer> zahlen) {
|
||||
//= Arrays.asList(1, 5, 8, 9, 12, 10, 3);
|
||||
int mitMap;
|
||||
|
||||
mitMap = zahlen.stream()
|
||||
.map(e -> e * e)
|
||||
.collect(summingInt(e -> e));
|
||||
return mitMap;
|
||||
}
|
||||
|
||||
public static int ohneMap(List<Integer> zahlen) {
|
||||
int ohneMap;
|
||||
ohneMap = zahlen.stream()
|
||||
.reduce((a, b) -> a + b * b)
|
||||
.orElse(0);
|
||||
return ohneMap;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,88 +0,0 @@
|
|||
package pr2.streams.reduce_critics;
|
||||
|
||||
/**
|
||||
* Computerspiel.
|
||||
*/
|
||||
public class ComputerGame {
|
||||
|
||||
/**
|
||||
* Name.
|
||||
*/
|
||||
private final String name;
|
||||
|
||||
/**
|
||||
* Jahr.
|
||||
*/
|
||||
private final int year;
|
||||
|
||||
/**
|
||||
* Metacritic.
|
||||
*/
|
||||
private final int metaScore;
|
||||
|
||||
/**
|
||||
* Userscore.
|
||||
*/
|
||||
private final int userScore;
|
||||
|
||||
/**
|
||||
* Legt ein neues Spiel an.
|
||||
*
|
||||
* @param name Name des Spiels.
|
||||
* @param year Jahr.
|
||||
* @param metaScore Metacritic-Bewertung.
|
||||
* @param userScore Nutzer-Bewertung.
|
||||
*/
|
||||
public ComputerGame(String name, int year, int metaScore, int userScore) {
|
||||
|
||||
this.name = name;
|
||||
this.year = year;
|
||||
this.metaScore = metaScore;
|
||||
this.userScore = userScore;
|
||||
}
|
||||
|
||||
/**
|
||||
* Name des Spiels.
|
||||
*
|
||||
* @return Der Name
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Jahr, in dem das Spiel erschienen ist bzw. kritisiert wurde.
|
||||
*
|
||||
* @return Das Erscheinungsjahr.
|
||||
*/
|
||||
public int getYear() {
|
||||
return year;
|
||||
}
|
||||
|
||||
/**
|
||||
* Die Metacritic-Bewertung (0-100).
|
||||
*
|
||||
* @return Die Bewertung.
|
||||
*/
|
||||
public int getMetaScore() {
|
||||
return metaScore;
|
||||
}
|
||||
|
||||
/**
|
||||
* Die Benutzerbewertung (0-100).
|
||||
*
|
||||
* @return Die Benutzerbewertung.
|
||||
*/
|
||||
public int getUserScore() {
|
||||
return userScore;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%d;%s;%s;%d", metaScore, name, userScore / 10.0,
|
||||
year);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,61 +0,0 @@
|
|||
package pr2.streams.reduce_critics;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Datenbank für die Spiele.
|
||||
*/
|
||||
public class GameDatabase {
|
||||
|
||||
private final List<ComputerGame> games;
|
||||
|
||||
/**
|
||||
* Datenbank mit Spieleinfomrationen.
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public GameDatabase() {
|
||||
games = new ArrayList<>();
|
||||
|
||||
try {
|
||||
BufferedReader br =
|
||||
new BufferedReader(
|
||||
new FileReader("games.txt"));
|
||||
|
||||
String line;
|
||||
|
||||
while ((line = br.readLine()) != null) {
|
||||
String[] tokens = line.split(";");
|
||||
int metacritic = Integer.parseInt(tokens[0]);
|
||||
String name = tokens[1];
|
||||
int userScore = (int) (Double.parseDouble(tokens[2]) * 10);
|
||||
Date date =
|
||||
new SimpleDateFormat("MMM d, yyyy", Locale.US).parse(
|
||||
tokens[3]);
|
||||
|
||||
games.add(new ComputerGame(name, date.getYear() + 1900,
|
||||
metacritic, userScore));
|
||||
}
|
||||
|
||||
br.close();
|
||||
} catch (IOException | ParseException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt die Liste der Spiele zurück.
|
||||
*
|
||||
* @return Liste der Spiele
|
||||
*/
|
||||
public List<ComputerGame> getGames() {
|
||||
return new ArrayList<>(games);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,41 +0,0 @@
|
|||
package pr2.streams.reduce_critics;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* Spiele auflisten.
|
||||
*/
|
||||
public class MapReduceGames {
|
||||
|
||||
/**
|
||||
* Hauptmethode.
|
||||
*
|
||||
* @param args Kommandozeilenargumente.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
GameDatabase db = new GameDatabase();
|
||||
List<ComputerGame> games = db.getGames();
|
||||
|
||||
// Abweichung des Metascores von dem Userscore
|
||||
// als quadratisches Mittel berechnen.
|
||||
Stream<ComputerGame> stream = games.stream();
|
||||
|
||||
double rms = stream.map(g -> g.getMetaScore() - g.getUserScore())
|
||||
.map(g -> g * g)
|
||||
.reduce(0, (sum, value) -> sum + value);
|
||||
rms /= games.size();
|
||||
rms = Math.sqrt(rms);
|
||||
|
||||
// Abweichung als arithmetisches Mittel
|
||||
stream = games.stream();
|
||||
double mittelwert = stream.map(
|
||||
g -> g.getMetaScore() - g.getUserScore())
|
||||
.reduce(0, (sum, value) -> sum + value);
|
||||
mittelwert /= games.size();
|
||||
|
||||
System.out.printf("Abweichung Metascore: Durchschnitt=%.2f%n",
|
||||
mittelwert);
|
||||
System.out.printf("Abweichung Metascore: RMS=%.2f%n", rms);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
package pr2.streams.summieren;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class SumUp {
|
||||
public static int sumUp(Stream<Integer> numbers) {
|
||||
int result;
|
||||
result = numbers.reduce(0, (x, y) -> x + y);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
package pr2.streams.word_count;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static java.util.stream.Collectors.groupingBy;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
public class WordCount {
|
||||
public static List<String> count(List<String> liste) {
|
||||
List<String> result;
|
||||
|
||||
result = liste.stream()
|
||||
.collect(groupingBy(s -> s))
|
||||
.entrySet().stream()
|
||||
.map(e -> e.getKey() + "->" + e.getValue().size())
|
||||
.collect(toList());
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
package pr2.streams.chars_1.test;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import pr2.streams.chars_1.CountLetters;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class CountLettersTest {
|
||||
@Test
|
||||
void testCount() {
|
||||
String s = "Dies ist ein String mit einer ganzen Reihe von "
|
||||
+ "Buchstaben.";
|
||||
|
||||
assertEquals(44, CountLetters.count(s));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,19 +0,0 @@
|
|||
package pr2.streams.chars_2.test;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import pr2.streams.chars_2.CountLetters;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class CountLettersTest {
|
||||
@Test
|
||||
void testCount() {
|
||||
List<String> liste = Arrays.asList("Hallo", "Welt", "wie", "geht",
|
||||
"es");
|
||||
|
||||
assertEquals(18, CountLetters.count(liste));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,19 +0,0 @@
|
|||
package pr2.streams.chars_3.test;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import pr2.streams.chars_3.CountLetters;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class CountLettersTest {
|
||||
@Test
|
||||
void testCount() {
|
||||
List<String> liste = Arrays.asList("Hallo", "Welt", "wie", "geht",
|
||||
"es");
|
||||
|
||||
assertEquals(16, CountLetters.count(liste));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
package pr2.streams.filter.test;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import pr2.streams.filter.FilterList;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class FilterListTest {
|
||||
|
||||
@Test
|
||||
void testFilter() {
|
||||
List<String> liste = Arrays.asList(
|
||||
"Hallo", "Welt", "Dampfschifffahrt", "Hundekuchen",
|
||||
"Fiction", "Java", "Visual Basic", "Drucker");
|
||||
|
||||
List<String> result = Arrays.asList(
|
||||
"hallo", "dampfschifffahrt", "hundekuchen",
|
||||
"fiction", "visual basic", "drucker");
|
||||
|
||||
assertEquals(result, FilterList.filter(liste));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,26 +0,0 @@
|
|||
package pr2.streams.reduce.test;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import pr2.streams.reduce.Squarer;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class SquarerTest {
|
||||
|
||||
@Test
|
||||
void testMitMap() {
|
||||
List<Integer> l = Arrays.asList(1, 5, 8, 9, 12, 10, 3);
|
||||
Assertions.assertEquals(424, Squarer.mitMap(l));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOhneMap() {
|
||||
List<Integer> l = Arrays.asList(1, 5, 8, 9, 12, 10, 3);
|
||||
assertEquals(424, Squarer.ohneMap(l));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
package pr2.streams.summieren.test;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import pr2.streams.summieren.SumUp;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class SumUpTest {
|
||||
|
||||
@Test
|
||||
void testSum() {
|
||||
List<Integer> l = Arrays.asList(1, 5, 8, 9, 12, 10, 3);
|
||||
assertEquals(48, SumUp.sumUp(l.stream()));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
package pr2.streams.word_count.test;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import pr2.streams.word_count.WordCount;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class WordCountTest {
|
||||
|
||||
@Test
|
||||
void testWordCount() {
|
||||
List<String> liste = Arrays.asList("A", "A", "B", "B", "B", "C",
|
||||
"D", "D", "E", "E", "E", "F");
|
||||
|
||||
assertEquals(Arrays.asList("A->2", "B->3", "C->1", "D->2",
|
||||
"E->3", "F->1"), WordCount.count(liste));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue