This commit is contained in:
2025-05-08 12:26:08 +02:00
parent 99f5105657
commit f65a95e5ac
5 changed files with 137 additions and 70 deletions

View File

@@ -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<String> filterMap(List<String> input) {
return input.stream().filter(c -> !c.contains("a"))
.map(String::toUpperCase).collect(Collectors.toList());
}
static int sumOfEven(List<Integer> input) {
return input.stream().filter(i -> i % 2 == 0).mapToInt(i -> i).sum();
}
static Map<String, Integer> mapColl(List<String> words) {
return words.stream().collect(Collectors.toMap(s -> s, String::length, (_, y) -> y));
}
static Integer getMax(List<Integer> input) {
return input.stream().mapToInt(i -> i).max().orElse(-1);
}
static List<String> reverseLengthSorting(List<String> input) {
return input.stream().sorted((x, y) -> y.length() - x.length()).collect(Collectors.toList());
}
static int countAbvTen(List<Integer> ints) {
return (int) ints.stream().filter(i -> i > 10).count();
}
static List<Integer> flatten(List<List<Integer>> nested) {
return nested.stream().flatMap(List::stream).toList();
}
public static Set<Integer> 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) {

View File

@@ -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<String> 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<String, List<Winner>> winnersByNationalityNames() {
return tdfWinners.stream().collect(Collectors.groupingBy(Winner::getNationality, Collectors.toList()));
// return tdfWinners.stream().collect(Collectors.groupingBy())
return null;
}

View File

@@ -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"));
}
}

View File

@@ -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<ProgressBar> bars = new ArrayList<>();
private static final int maxSleepTime = 10;
private static final int maxStepSize = 10;
private static final List<Double> horses = new ArrayList<>();
private static boolean lock = false;
protected static final TextArea textArea = new TextArea();
protected static final ArrayList<Horse> horses = new ArrayList<>();
private static final ArrayList<Thread> threads = new ArrayList<>();
private static boolean lock = false, reset = false;
public static void main(String[] args) {
launch(args);
}
private static void startRace() {
// ArrayList<Thread>
}
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();
});

View File

@@ -1,5 +0,0 @@
package UE20_290425_Threads2;
public class horse {
}