From f65a95e5ac29ee4eccecd1cbb09b6d003c8a45ab Mon Sep 17 00:00:00 2001 From: Alexander Bachinger Date: Thu, 8 May 2025 12:26:08 +0200 Subject: [PATCH] UE20 --- src/UE15_030325_Streams2/UE15_Streams2.java | 74 ++++++++++++++----- src/UE16_110325_Streams3/UE16_Streams3.java | 6 +- src/UE20_290425_Threads2/Horse.java | 40 ++++++++++ src/UE20_290425_Threads2/Pferderennen.java | 82 ++++++++++----------- src/UE20_290425_Threads2/horse.java | 5 -- 5 files changed, 137 insertions(+), 70 deletions(-) create mode 100644 src/UE20_290425_Threads2/Horse.java delete mode 100644 src/UE20_290425_Threads2/horse.java diff --git a/src/UE15_030325_Streams2/UE15_Streams2.java b/src/UE15_030325_Streams2/UE15_Streams2.java index 27f62c7..7bbdeec 100644 --- a/src/UE15_030325_Streams2/UE15_Streams2.java +++ b/src/UE15_030325_Streams2/UE15_Streams2.java @@ -1,26 +1,66 @@ package UE15_030325_Streams2; -import java.util.Arrays; -import java.util.Map; -import java.util.Random; -import java.util.Set; +import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.IntStream; public class UE15_Streams2 { public static void main(String[] args) { - System.out.println(lottoziehung()); - System.out.println(berechnePI(-5)); - System.out.println(berechnePI(1)); - System.out.println(berechnePI(2)); - System.out.println(berechnePI(3)); - System.out.println(berechnePI(1001)); - System.out.println(zaehleZeichenAusVorrat("Hallo 1AI", "aeiou")); - System.out.println(zaehleZeichenAusVorrat("*code*123#", "?%*!#$")); - System.out.println(createRandoString("1234", 5)); - System.out.println(Arrays.toString(deleteEquals(new int[]{1, 3, 3, 1, 2, 1, 5}))); - System.out.println(zeichenStatistik("Hallo Welt wie geht es heute")); +// System.out.println(lottoziehung()); +// System.out.println(berechnePI(-5)); +// System.out.println(berechnePI(1)); +// System.out.println(berechnePI(2)); +// System.out.println(berechnePI(3)); +// System.out.println(berechnePI(1001)); +// System.out.println(zaehleZeichenAusVorrat("Hallo 1AI", "aeiou")); +// System.out.println(zaehleZeichenAusVorrat("*code*123#", "?%*!#$")); +// System.out.println(createRandoString("1234", 5)); +// System.out.println(Arrays.toString(deleteEquals(new int[]{1, 3, 3, 1, 2, 1, 5}))); +// System.out.println(zeichenStatistik("Hallo Welt wie geht es heute")); + +// System.out.println("hallo du 2kek!".chars().mapToObj(c -> (char) c).filter(Character::isAlphabetic).map(Object::toString).collect(Collectors.joining())); + +// System.out.println(Arrays.stream("hallo du du 2kek!".split(" ")).distinct().toList()); + + System.out.printf("Filtered and Mapped: %s\n", filterMap(Arrays.asList("hell", "highway", "figure.09", "in the end", "busfahrt"))); + System.out.printf("Sum of the even Elements: %d\n", sumOfEven(Arrays.asList(1, 5, 22, 4, 6, 44, 7, 45))); + System.out.printf("Mapping between word and its length: %s\n", mapColl(Arrays.asList("word", "and", "another", "one", "one"))); + System.out.printf("Maximum: %d%n", getMax(Arrays.asList(1, 5, 2, 3, 5, 7, 44, 3, 6, 2))); + System.out.printf("Reverse sorted List: %s%n", reverseLengthSorting(Arrays.asList("a", "aa", "bbb", "ejcjiqejifoew"))); + System.out.printf("Flattened List: %s%n", flatten(Arrays.asList(Arrays.asList(1, 2, 3), Arrays.asList(4, 5, 6)))); + System.out.printf("Count of Elements > 10: %d%n", countAbvTen(Arrays.asList(1,55,3,788,4,654,3,664,55,78,5))); + + } + + static List filterMap(List input) { + return input.stream().filter(c -> !c.contains("a")) + .map(String::toUpperCase).collect(Collectors.toList()); + } + + static int sumOfEven(List input) { + return input.stream().filter(i -> i % 2 == 0).mapToInt(i -> i).sum(); + } + + static Map mapColl(List words) { + return words.stream().collect(Collectors.toMap(s -> s, String::length, (_, y) -> y)); + } + + static Integer getMax(List input) { + return input.stream().mapToInt(i -> i).max().orElse(-1); + } + + static List reverseLengthSorting(List input) { + return input.stream().sorted((x, y) -> y.length() - x.length()).collect(Collectors.toList()); + } + + static int countAbvTen(List ints) { + return (int) ints.stream().filter(i -> i > 10).count(); + } + + + static List flatten(List> nested) { + return nested.stream().flatMap(List::stream).toList(); } public static Set lottoziehung() { @@ -37,9 +77,7 @@ public class UE15_Streams2 { } public static String createRandoString(String vorrat, int len) { - return IntStream.range(0, len).mapToObj( - - _ -> String.valueOf(vorrat.charAt(new Random().nextInt(vorrat.length())))).collect(Collectors.joining()); + return IntStream.range(0, len).mapToObj(_ -> String.valueOf(vorrat.charAt(new Random().nextInt(vorrat.length())))).collect(Collectors.joining()); } public static int[] deleteEquals(int[] ia) { diff --git a/src/UE16_110325_Streams3/UE16_Streams3.java b/src/UE16_110325_Streams3/UE16_Streams3.java index 4d11a0a..d0f6db4 100644 --- a/src/UE16_110325_Streams3/UE16_Streams3.java +++ b/src/UE16_110325_Streams3/UE16_Streams3.java @@ -36,7 +36,7 @@ public class UE16_Streams3 { System.out.println("18. winnersByNationality - " + winnersByNationalityNames()); System.out.println("19. winsByNationalityCounting - " + winsByNationalityCounting()); - printLogStatistic(Path.of("src/UE16_110325_Stream3/access_log/access.log")); +// printLogStatistic(Path.of("src/UE16_110325_Stream3/access_log/access.log")); } public static void printLogStatistic(Path path) { @@ -60,7 +60,6 @@ public class UE16_Streams3 { return objects.entrySet().stream().max(Map.Entry.comparingByValue()).map(Map.Entry::getKey).orElse(null); } - public static String getMostCommonIP(List lines) { return mostCommonByRegex("(?:\\S+\\s+){2}(\\d{1,3}(?:\\.\\d{1,3}){3})", lines); } @@ -152,7 +151,8 @@ public class UE16_Streams3 { } public static Map> winnersByNationalityNames() { - return tdfWinners.stream().collect(Collectors.groupingBy(Winner::getNationality, Collectors.toList())); +// return tdfWinners.stream().collect(Collectors.groupingBy()) + return null; } diff --git a/src/UE20_290425_Threads2/Horse.java b/src/UE20_290425_Threads2/Horse.java new file mode 100644 index 0000000..a3d3c88 --- /dev/null +++ b/src/UE20_290425_Threads2/Horse.java @@ -0,0 +1,40 @@ +package UE20_290425_Threads2; + +import javafx.application.Platform; +import javafx.scene.control.ProgressBar; + +import static UE20_290425_Threads2.Pferderennen.textArea; + +public class Horse extends ProgressBar implements Runnable { + private static final double finish_line = 1_000_000; + private static final double maxSleepTime = 100, maxStepSize = 10_000; + private final int index; + private double pos = 0; + + public Horse(double var, int index) { + super(var); + this.index = index; + } + + public double getPos() { + return this.pos; + } + + public void setPos(double pos) { + this.pos = pos; + } + + @Override + public void run() { + while (pos < finish_line) { + pos += (int) (Math.random() * maxStepSize + 1); + Platform.runLater(() -> this.setProgress(Math.min(pos / finish_line, 1.0))); + try { + Thread.sleep((int) (Math.random() * maxSleepTime + 1)); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + Platform.runLater(() -> textArea.appendText("Horse/Thread " + index + " ist im Ziel\n")); + } +} diff --git a/src/UE20_290425_Threads2/Pferderennen.java b/src/UE20_290425_Threads2/Pferderennen.java index abd5703..ec4b5e2 100644 --- a/src/UE20_290425_Threads2/Pferderennen.java +++ b/src/UE20_290425_Threads2/Pferderennen.java @@ -1,6 +1,7 @@ package UE20_290425_Threads2; import javafx.application.Application; +import javafx.application.Platform; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Button; @@ -12,40 +13,35 @@ import javafx.scene.layout.VBox; import javafx.stage.Stage; import java.util.ArrayList; -import java.util.List; public class Pferderennen extends Application { - private static final ArrayList bars = new ArrayList<>(); - private static final int maxSleepTime = 10; - private static final int maxStepSize = 10; - private static final List horses = new ArrayList<>(); - private static boolean lock = false; + protected static final TextArea textArea = new TextArea(); + protected static final ArrayList horses = new ArrayList<>(); + private static final ArrayList threads = new ArrayList<>(); + private static boolean lock = false, reset = false; public static void main(String[] args) { launch(args); } private static void startRace() { -// ArrayList - } - - private static void endRace() { - lock = false; - } - - public static int random(int max) { - return (int) (Math.random() * max + 1); - } - - private synchronized void addToHorse(int horseIndex) { - int step = random(maxStepSize); - int sleep = random(maxSleepTime); - horses.set(horseIndex, horses.get(horseIndex) + step); - try { - Thread.sleep(sleep); - } catch (InterruptedException e) { - throw new RuntimeException(e); + threads.clear(); + for (Horse horse : horses) { + Thread thread = new Thread(horse); + threads.add(thread); + thread.start(); } + new Thread(() -> { + for (Thread t : threads) + try { + t.join(); + } catch (InterruptedException ignored) { + } + Platform.runLater(() -> { + textArea.appendText("\nFinish!\n"); + lock = false; + }); + }).start(); } @Override @@ -57,7 +53,6 @@ public class Pferderennen extends Application { backPane.setTop(titel); // BorderPane.setAlignment(titel, CENTER); - // Links: VBox mit zwei Buttons (+ / -) VBox button_box = new VBox(10); // button_box.setPadding(new Insets(10)); Button button_plus = new Button("+"); @@ -73,40 +68,39 @@ public class Pferderennen extends Application { backPane.setRight(start_box); // VBox.setMargin(button_start, new Insets(5)); - TextArea textArea = new TextArea(); textArea.setPrefRowCount(10); textArea.setEditable(false); backPane.setBottom(textArea); VBox horse_bars = new VBox(10); - for (ProgressBar bar : bars) horse_bars.getChildren().add(bar); + for (ProgressBar horse : horses) horse_bars.getChildren().add(horse); backPane.setCenter(horse_bars); button_plus.setOnAction(_ -> { - if (lock) { - textArea.appendText("Please wait for the race to finish\n"); - return; - } - ProgressBar bar = new ProgressBar(0); - bar.setMaxWidth(Double.MAX_VALUE); - bars.add(bar); - horse_bars.getChildren().add(bar); - textArea.appendText("Adding " + bars.size() + " Bars\n"); + if (lock) return; + Horse horse = new Horse(0, horses.size() + 1); + horse.setMaxWidth(Double.MAX_VALUE); + horses.add(horse); + horse_bars.getChildren().add(horse); + textArea.appendText("Adding " + horses.size() + " Bars\n"); }); button_minus.setOnAction(_ -> { - if (lock) { - textArea.appendText("Please wait for the race to finish\n"); - return; - } - if (!bars.isEmpty()) { - horse_bars.getChildren().remove(bars.removeLast()); - textArea.appendText("Adding " + bars.size() + " Bars\n"); + if (lock) return; + if (!horses.isEmpty()) { + horse_bars.getChildren().remove(horses.removeLast()); + textArea.appendText("Adding " + horses.size() + " Bars\n"); } }); button_start.setOnAction(_ -> { + if (lock) { + textArea.appendText("Please wait for the race to finish\n"); + return; + } + for (Horse horse : horses) horse.setProgress(0); lock = true; + textArea.appendText("Ergebnis:\n"); startRace(); }); diff --git a/src/UE20_290425_Threads2/horse.java b/src/UE20_290425_Threads2/horse.java deleted file mode 100644 index ab6ff5f..0000000 --- a/src/UE20_290425_Threads2/horse.java +++ /dev/null @@ -1,5 +0,0 @@ -package UE20_290425_Threads2; - -public class horse { - -}