UE20
This commit is contained in:
@@ -1,26 +1,66 @@
|
|||||||
package UE15_030325_Streams2;
|
package UE15_030325_Streams2;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.*;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Random;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.IntStream;
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
public class UE15_Streams2 {
|
public class UE15_Streams2 {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println(lottoziehung());
|
// System.out.println(lottoziehung());
|
||||||
System.out.println(berechnePI(-5));
|
// System.out.println(berechnePI(-5));
|
||||||
System.out.println(berechnePI(1));
|
// System.out.println(berechnePI(1));
|
||||||
System.out.println(berechnePI(2));
|
// System.out.println(berechnePI(2));
|
||||||
System.out.println(berechnePI(3));
|
// System.out.println(berechnePI(3));
|
||||||
System.out.println(berechnePI(1001));
|
// System.out.println(berechnePI(1001));
|
||||||
System.out.println(zaehleZeichenAusVorrat("Hallo 1AI", "aeiou"));
|
// System.out.println(zaehleZeichenAusVorrat("Hallo 1AI", "aeiou"));
|
||||||
System.out.println(zaehleZeichenAusVorrat("*code*123#", "?%*!#$"));
|
// System.out.println(zaehleZeichenAusVorrat("*code*123#", "?%*!#$"));
|
||||||
System.out.println(createRandoString("1234", 5));
|
// System.out.println(createRandoString("1234", 5));
|
||||||
System.out.println(Arrays.toString(deleteEquals(new int[]{1, 3, 3, 1, 2, 1, 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(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() {
|
public static Set<Integer> lottoziehung() {
|
||||||
@@ -37,9 +77,7 @@ public class UE15_Streams2 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static String createRandoString(String vorrat, int len) {
|
public static String createRandoString(String vorrat, int len) {
|
||||||
return IntStream.range(0, len).mapToObj(
|
return IntStream.range(0, len).mapToObj(_ -> String.valueOf(vorrat.charAt(new Random().nextInt(vorrat.length())))).collect(Collectors.joining());
|
||||||
|
|
||||||
_ -> String.valueOf(vorrat.charAt(new Random().nextInt(vorrat.length())))).collect(Collectors.joining());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int[] deleteEquals(int[] ia) {
|
public static int[] deleteEquals(int[] ia) {
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ public class UE16_Streams3 {
|
|||||||
System.out.println("18. winnersByNationality - " + winnersByNationalityNames());
|
System.out.println("18. winnersByNationality - " + winnersByNationalityNames());
|
||||||
System.out.println("19. winsByNationalityCounting - " + winsByNationalityCounting());
|
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) {
|
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);
|
return objects.entrySet().stream().max(Map.Entry.comparingByValue()).map(Map.Entry::getKey).orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static String getMostCommonIP(List<String> lines) {
|
public static String getMostCommonIP(List<String> lines) {
|
||||||
return mostCommonByRegex("(?:\\S+\\s+){2}(\\d{1,3}(?:\\.\\d{1,3}){3})", 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() {
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
40
src/UE20_290425_Threads2/Horse.java
Normal file
40
src/UE20_290425_Threads2/Horse.java
Normal 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"));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package UE20_290425_Threads2;
|
package UE20_290425_Threads2;
|
||||||
|
|
||||||
import javafx.application.Application;
|
import javafx.application.Application;
|
||||||
|
import javafx.application.Platform;
|
||||||
import javafx.geometry.Insets;
|
import javafx.geometry.Insets;
|
||||||
import javafx.scene.Scene;
|
import javafx.scene.Scene;
|
||||||
import javafx.scene.control.Button;
|
import javafx.scene.control.Button;
|
||||||
@@ -12,40 +13,35 @@ import javafx.scene.layout.VBox;
|
|||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class Pferderennen extends Application {
|
public class Pferderennen extends Application {
|
||||||
private static final ArrayList<ProgressBar> bars = new ArrayList<>();
|
protected static final TextArea textArea = new TextArea();
|
||||||
private static final int maxSleepTime = 10;
|
protected static final ArrayList<Horse> horses = new ArrayList<>();
|
||||||
private static final int maxStepSize = 10;
|
private static final ArrayList<Thread> threads = new ArrayList<>();
|
||||||
private static final List<Double> horses = new ArrayList<>();
|
private static boolean lock = false, reset = false;
|
||||||
private static boolean lock = false;
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
launch(args);
|
launch(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void startRace() {
|
private static void startRace() {
|
||||||
// ArrayList<Thread>
|
threads.clear();
|
||||||
}
|
for (Horse horse : horses) {
|
||||||
|
Thread thread = new Thread(horse);
|
||||||
private static void endRace() {
|
threads.add(thread);
|
||||||
lock = false;
|
thread.start();
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
new Thread(() -> {
|
||||||
|
for (Thread t : threads)
|
||||||
|
try {
|
||||||
|
t.join();
|
||||||
|
} catch (InterruptedException ignored) {
|
||||||
|
}
|
||||||
|
Platform.runLater(() -> {
|
||||||
|
textArea.appendText("\nFinish!\n");
|
||||||
|
lock = false;
|
||||||
|
});
|
||||||
|
}).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -57,7 +53,6 @@ public class Pferderennen extends Application {
|
|||||||
backPane.setTop(titel);
|
backPane.setTop(titel);
|
||||||
// BorderPane.setAlignment(titel, CENTER);
|
// BorderPane.setAlignment(titel, CENTER);
|
||||||
|
|
||||||
// Links: VBox mit zwei Buttons (+ / -)
|
|
||||||
VBox button_box = new VBox(10);
|
VBox button_box = new VBox(10);
|
||||||
// button_box.setPadding(new Insets(10));
|
// button_box.setPadding(new Insets(10));
|
||||||
Button button_plus = new Button("+");
|
Button button_plus = new Button("+");
|
||||||
@@ -73,40 +68,39 @@ public class Pferderennen extends Application {
|
|||||||
backPane.setRight(start_box);
|
backPane.setRight(start_box);
|
||||||
// VBox.setMargin(button_start, new Insets(5));
|
// VBox.setMargin(button_start, new Insets(5));
|
||||||
|
|
||||||
TextArea textArea = new TextArea();
|
|
||||||
textArea.setPrefRowCount(10);
|
textArea.setPrefRowCount(10);
|
||||||
textArea.setEditable(false);
|
textArea.setEditable(false);
|
||||||
backPane.setBottom(textArea);
|
backPane.setBottom(textArea);
|
||||||
|
|
||||||
VBox horse_bars = new VBox(10);
|
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);
|
backPane.setCenter(horse_bars);
|
||||||
|
|
||||||
button_plus.setOnAction(_ -> {
|
button_plus.setOnAction(_ -> {
|
||||||
if (lock) {
|
if (lock) return;
|
||||||
textArea.appendText("Please wait for the race to finish\n");
|
Horse horse = new Horse(0, horses.size() + 1);
|
||||||
return;
|
horse.setMaxWidth(Double.MAX_VALUE);
|
||||||
}
|
horses.add(horse);
|
||||||
ProgressBar bar = new ProgressBar(0);
|
horse_bars.getChildren().add(horse);
|
||||||
bar.setMaxWidth(Double.MAX_VALUE);
|
textArea.appendText("Adding " + horses.size() + " Bars\n");
|
||||||
bars.add(bar);
|
|
||||||
horse_bars.getChildren().add(bar);
|
|
||||||
textArea.appendText("Adding " + bars.size() + " Bars\n");
|
|
||||||
});
|
});
|
||||||
button_minus.setOnAction(_ -> {
|
button_minus.setOnAction(_ -> {
|
||||||
if (lock) {
|
if (lock) return;
|
||||||
textArea.appendText("Please wait for the race to finish\n");
|
if (!horses.isEmpty()) {
|
||||||
return;
|
horse_bars.getChildren().remove(horses.removeLast());
|
||||||
}
|
textArea.appendText("Adding " + horses.size() + " Bars\n");
|
||||||
if (!bars.isEmpty()) {
|
|
||||||
horse_bars.getChildren().remove(bars.removeLast());
|
|
||||||
textArea.appendText("Adding " + bars.size() + " Bars\n");
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
button_start.setOnAction(_ -> {
|
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;
|
lock = true;
|
||||||
|
textArea.appendText("Ergebnis:\n");
|
||||||
startRace();
|
startRace();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
package UE20_290425_Threads2;
|
|
||||||
|
|
||||||
public class horse {
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user