98 lines
3.5 KiB
Java
98 lines
3.5 KiB
Java
package UE14_130225_Streams;
|
|
|
|
import java.util.Random;
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
import java.util.stream.IntStream;
|
|
|
|
public class UE14_Streams {
|
|
public static void main(String[] args) {
|
|
zahlenreihen();
|
|
|
|
printFolgeOhne3(1);
|
|
printFolgeOhne3(5);
|
|
printFolgeOhne3(10);
|
|
printFolgeOhne3(20);
|
|
|
|
for (int i = 1; i <= 6; i++) wuerfelStatistik(i);
|
|
|
|
for (int i = 0; i <= 100; i++) alleTeiler(i);
|
|
|
|
for (int i = 0; i <= 100; i++)
|
|
if (istPrimzahl(i)) System.out.println(i + ": is a Prime Number");
|
|
else System.out.println(i + ": is NOT Prime Number");
|
|
|
|
for (int i = 0; i <= 10; i++) System.out.println(i + ": " + gaussSumme(i));
|
|
|
|
verticalString("Hallo");
|
|
verticalString("Hallo!!!");
|
|
verticalString("a(&\t/'?%\n§''%Z??/§$%");
|
|
|
|
mitStern("Hallo");
|
|
System.out.println();
|
|
mitStern("Hallo!!!");
|
|
System.out.println();
|
|
mitStern("a(&/'?%§''%Z??/§$%");
|
|
System.out.println();
|
|
|
|
deleteBlanks("Hallo Welt wie gehts");
|
|
System.out.println();
|
|
|
|
System.out.println(enthaeltZiffern("Hallo 3AI"));
|
|
System.out.println(enthaeltZiffern("Hallo Welt"));
|
|
System.out.println(enthaeltZiffern("0"));
|
|
System.out.println(enthaeltZiffern("A"));
|
|
System.out.println(enthaeltZiffern(""));
|
|
}
|
|
|
|
public static void zahlenreihen() {
|
|
IntStream.rangeClosed(1, 25).forEach(n -> System.out.print(n + " "));
|
|
System.out.println();
|
|
IntStream.rangeClosed(1, 25).filter(n -> n % 2 != 0).forEach(n -> System.out.print(n + " "));
|
|
System.out.println();
|
|
IntStream.iterate(20, n -> n - 5).limit(9).forEach(n -> System.out.print(n + " "));
|
|
System.out.println();
|
|
IntStream.iterate(1, n -> n * 2).limit(7).forEach(n -> System.out.print(n + " "));
|
|
System.out.println();
|
|
AtomicInteger power = new AtomicInteger(1);
|
|
IntStream.iterate(2, n -> n + power.getAndUpdate(x -> x * 2)).limit(8).forEach(n -> System.out.print(n + " "));
|
|
System.out.println();
|
|
}
|
|
|
|
public static void printFolgeOhne3(int anz) {
|
|
IntStream.iterate(4, n -> n + 1).filter(n -> n % 3 != 0).limit(anz).forEach(n -> System.out.print(n + " "));
|
|
System.out.println();
|
|
}
|
|
|
|
public static void wuerfelStatistik(int augenzahl) {
|
|
System.out.println(augenzahl + ": " + new Random().ints(1000, 1, 7).filter(n -> n == augenzahl).count());
|
|
}
|
|
|
|
public static void alleTeiler(int z) {
|
|
System.out.println(z + ": " + IntStream.rangeClosed(1, z).filter(n -> z % n == 0).mapToObj(String::valueOf).reduce((a, b) -> a + ", " + b).orElse(""));
|
|
}
|
|
|
|
public static boolean istPrimzahl(int z) {
|
|
// return z > 1 && IntStream.rangeClosed(2, z).filter(n -> z % n == 0).count() == 1;
|
|
return z > 1 && IntStream.rangeClosed(2, (int) Math.sqrt(z)).allMatch(n -> z % n != 0); // More Efficient (from 10-12ms to 5-8ms)
|
|
}
|
|
|
|
public static int gaussSumme(int n) {
|
|
return IntStream.rangeClosed(1, n).sum();
|
|
}
|
|
|
|
public static void verticalString(String s) {
|
|
s.chars().forEach(c -> System.out.println((char) c));
|
|
}
|
|
|
|
public static void mitStern(String s) {
|
|
s.chars().forEach(c -> System.out.print((char) c + "*"));
|
|
}
|
|
|
|
public static void deleteBlanks(String s1) {
|
|
s1.chars().filter(c -> c != ' ').forEach(c -> System.out.print((char) c));
|
|
}
|
|
|
|
public static boolean enthaeltZiffern(String s) {
|
|
return s.chars().filter(Character::isDigit).findAny().isPresent();
|
|
}
|
|
} |