lernen
parent
e8a38a501e
commit
c3387c13e6
|
@ -1,6 +1,6 @@
|
|||
package LinkedList.Generic;
|
||||
|
||||
public class Node <T> {
|
||||
T value;
|
||||
Node<T> next;
|
||||
}
|
||||
package LinkedList.Generic;
|
||||
|
||||
public class Node <T> {
|
||||
T value;
|
||||
Node<T> next;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,70 @@
|
|||
package VorlesungsFolien.SortierenmitComparatoren_Comparable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.TreeSet;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
//1.Ungeordnet//
|
||||
|
||||
/* Also wenn ich [1,2,4,5] speicher,
|
||||
* kann das sein, dass ich die Zahlen als Ausgabe so
|
||||
* kriege: [2,4,5,1] oder [4,5,1,2] etc...
|
||||
*
|
||||
*/
|
||||
|
||||
HashSet<Integer> set = new HashSet<>();
|
||||
set.add(1);
|
||||
set.add(1);
|
||||
set.add(2);
|
||||
|
||||
// Die Reihenfolge ist nicht garantiert
|
||||
System.out.println("Unordered Set: " + set);
|
||||
|
||||
|
||||
//####################################################################
|
||||
|
||||
//2. Geordnet//
|
||||
|
||||
/* Die Ausgabe ist immer: [2,1,4]
|
||||
* => eine geordnete Liste kann (muss aber nicht) sortiert sein
|
||||
*/
|
||||
ArrayList<Integer> list = new ArrayList<>();
|
||||
list.add(2);
|
||||
list.add(1);
|
||||
list.add(4);
|
||||
|
||||
// Die Reihenfolge bleibt gleich wie eingefügt
|
||||
System.out.println("Ordered List: " + list);
|
||||
|
||||
|
||||
|
||||
|
||||
//####################################################################
|
||||
/* sortiert (sorted) → Die Reihenfolge der Elemente folgt einem Sortierkriterium
|
||||
* => eine sortierte Collection ist immer geordnet
|
||||
* => eine geordnete Liste kann (muss aber nicht) sortiert sein
|
||||
*/
|
||||
TreeSet<Integer> sortedSet = new TreeSet<>();
|
||||
sortedSet.add(1);
|
||||
sortedSet.add(4);
|
||||
sortedSet.add(3);
|
||||
sortedSet.add(4);
|
||||
|
||||
// Elemente werden alphabetisch sortiert
|
||||
System.out.println("Sorted Set: " + sortedSet);
|
||||
|
||||
|
||||
TreeSet<String> ts = new TreeSet<>();
|
||||
ts.add("X");
|
||||
ts.add("B");
|
||||
ts.add("A");
|
||||
ts.add("H");
|
||||
for (String s : ts)
|
||||
System.out.println(s);
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
package VorlesungsFolien.SortierenmitComparatoren_Comparable;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Produkt {
|
||||
private String name;
|
||||
private double preis;
|
||||
|
||||
public Produkt(String name, double preis) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.preis = preis;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Produkt [name=" + name + ", preis=" + preis + "]";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
ArrayList<Produkt> arr = new ArrayList<>();
|
||||
|
||||
arr.add(new Produkt("d", 12.0));
|
||||
arr.add(new Produkt("c", 15.0));
|
||||
arr.add(new Produkt("a", 13.0));
|
||||
arr.add(new Produkt("b", 11.0));
|
||||
|
||||
Comparator<Produkt> preisAufsteigend = new Comparator<>() {
|
||||
|
||||
@Override
|
||||
public int compare(Produkt o1, Produkt o2) {
|
||||
if (o1.preis > o2.preis)
|
||||
return 1;
|
||||
|
||||
else if (o1.preis == o2.preis)
|
||||
return 0;
|
||||
|
||||
else
|
||||
return -1;
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
Comparator<Produkt> nameAlphabetisch = new Comparator<>() {
|
||||
|
||||
@Override
|
||||
public int compare(Produkt o1, Produkt o2) {
|
||||
int erg = o1.name.compareTo(o2.name);
|
||||
return erg * -1;
|
||||
}
|
||||
|
||||
};
|
||||
Collections.sort(arr, nameAlphabetisch);
|
||||
for (Produkt p : arr)
|
||||
System.out.println(p.toString());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
package VorlesungsFolien.SortierenmitComparatoren_Comparable;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Student {
|
||||
private int matrikelNummer;
|
||||
private String name;
|
||||
|
||||
// Konstruktor
|
||||
public Student(int matrikelNummer, String name) {
|
||||
this.matrikelNummer = matrikelNummer;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
// toString-Methode
|
||||
public String toString() {
|
||||
return matrikelNummer + " - " + name;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
Comparator<Student> com = new Comparator<>() {
|
||||
|
||||
@Override
|
||||
public int compare(Student o1, Student o2) {
|
||||
if (o1.matrikelNummer%10 > o2.matrikelNummer%10)
|
||||
return 1;
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
Comparator<Student> name = new Comparator<>() {
|
||||
|
||||
@Override
|
||||
public int compare(Student o1, Student o2) {
|
||||
if (o1.name.charAt(1) > o2.name.charAt(1))
|
||||
return 1;
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
List<Student> studenten = new ArrayList<>();
|
||||
studenten.add(new Student(4411,"AH"));
|
||||
studenten.add(new Student(5714,"AA"));
|
||||
studenten.add(new Student(4713,"AC"));
|
||||
|
||||
Collections.sort(studenten,name);
|
||||
for (Student s : studenten)
|
||||
System.out.println(s);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -1,49 +1,40 @@
|
|||
package oop.Enumeration;
|
||||
|
||||
public class Main {
|
||||
|
||||
// وضعنا فيه 7 ثوابت Days إسمه enum هنا قمنا بتعريف
|
||||
enum Days {
|
||||
MONDAY,
|
||||
TUESDAY,
|
||||
WEDNESDAY,
|
||||
THURSDAY,
|
||||
FRIDAY,
|
||||
SATURDAY,
|
||||
SUNDAY
|
||||
}
|
||||
|
||||
enum Size {
|
||||
|
||||
// هنا قمنا بتعريف أسماء الثوابت و تحديد قيمهم
|
||||
SMALL(100), MEDIUM(150), LARGE(200), XLARGE(250);
|
||||
|
||||
private int value;
|
||||
|
||||
private Size(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
// .values => Gibt alle Konstaten an
|
||||
for (Days d : Days.values())
|
||||
System.out.println(d);
|
||||
|
||||
// .ordinal => Gibt das Index der Konstante zurück
|
||||
System.out.println(Days.MONDAY.ordinal());
|
||||
|
||||
// Hier speichere ich eine Kosntante
|
||||
Days day = Days.THURSDAY;
|
||||
System.out.println(day);
|
||||
|
||||
System.out.println();
|
||||
System.out.println("Die Sizen: ");
|
||||
for (Size s : Size.values()) {
|
||||
System.out.println(s + " " + s.value);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
package oop.Enumeration;
|
||||
|
||||
public class Main {
|
||||
|
||||
// وضعنا فيه 7 ثوابت Days إسمه enum هنا قمنا بتعريف
|
||||
enum Days {
|
||||
MONDAY,
|
||||
TUESDAY,
|
||||
WEDNESDAY,
|
||||
THURSDAY,
|
||||
FRIDAY,
|
||||
SATURDAY,
|
||||
SUNDAY;
|
||||
|
||||
public void test() {
|
||||
System.out.println("Test");
|
||||
}
|
||||
}
|
||||
|
||||
enum Size {
|
||||
|
||||
// هنا قمنا بتعريف أسماء الثوابت و تحديد قيمهم
|
||||
SMALL(100), MEDIUM(150), LARGE(200), XLARGE(250);
|
||||
|
||||
private int value;
|
||||
|
||||
private Size(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Days.FRIDAY.test();
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,71 +1,82 @@
|
|||
package streams;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class Aufgaben {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
// List<Integer> numbers = Arrays.asList(1,2,3,4,5);
|
||||
//
|
||||
// List<Integer> result = numbers.stream()
|
||||
// .map(i -> i * i)
|
||||
// .collect(Collectors.toList());
|
||||
//
|
||||
// System.out.println(result);
|
||||
|
||||
// List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David", "Eva");
|
||||
// Integer result2 = names
|
||||
// .stream()
|
||||
// .map(e -> e.length())
|
||||
// .max((integer, newInteger) -> Integer.compare(integer, newInteger))
|
||||
// .get();
|
||||
// System.out.println(result2);
|
||||
//
|
||||
// List<Integer> numbers = Arrays.asList(1,2,3,4,5);
|
||||
//
|
||||
// Integer max = numbers.stream()
|
||||
// .max((a,b) -> a.compareTo(b))
|
||||
// .get();
|
||||
//
|
||||
// System.out.println(max);
|
||||
|
||||
String str = "Die Katze jagt die Maus";
|
||||
Stream<String> wörterString = Arrays.stream(str.split(" "));
|
||||
|
||||
|
||||
|
||||
|
||||
List<String> saetze = Arrays.asList(
|
||||
"Die Katze jagt die Maus",
|
||||
"ist klein",
|
||||
"Der Hund ",
|
||||
"Die Katze schläft Bellt"
|
||||
);
|
||||
|
||||
//finde das kleinste Wort
|
||||
|
||||
String maxWort = saetze.stream()
|
||||
.flatMap(satz -> Arrays.stream(satz.split(" "))) // Teilt jeden Satz in Wörter
|
||||
.max(String::compareTo) // finde das lexikografisch größte Wort
|
||||
.get();
|
||||
|
||||
// System.out.println("maxWort " + maxWort);
|
||||
|
||||
// speichere jede Wort der nur 5 Zeichen enthalten und alle Kleinbuchstaben
|
||||
List<String> mindestensfünfZeichen = saetze.stream()
|
||||
.flatMap(satz -> Arrays.stream(satz.split(" ")))
|
||||
.filter(wort -> wort.length() == 5)
|
||||
.map(wort -> wort.toLowerCase())
|
||||
.toList();
|
||||
|
||||
//System.out.println(mindestensfünfZeichen);
|
||||
|
||||
|
||||
saetze.stream()
|
||||
.flatMap(satz -> Arrays.stream(satz.split(" ")))
|
||||
.forEach(wort -> System.out.println("Wort: " + wort + " Wortlänge " + wort.length()));
|
||||
|
||||
}
|
||||
}
|
||||
package streams;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class Aufgaben {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
// List<Integer> numbers = Arrays.asList(1,2,3,4,5);
|
||||
//
|
||||
// List<Integer> result = numbers.stream()
|
||||
// .map(i -> i * i)
|
||||
// .collect(Collectors.toList());
|
||||
//
|
||||
// System.out.println(result);
|
||||
|
||||
// List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David", "Eva");
|
||||
// Integer result2 = names
|
||||
// .stream()
|
||||
// .map(e -> e.length())
|
||||
// .max((integer, newInteger) -> Integer.compare(integer, newInteger))
|
||||
// .get();
|
||||
// System.out.println(result2);
|
||||
//
|
||||
// List<Integer> numbers = Arrays.asList(1,2,3,4,5);
|
||||
//
|
||||
// Integer max = numbers.stream()
|
||||
// .max((a,b) -> a.compareTo(b))
|
||||
// .get();
|
||||
//
|
||||
// System.out.println(max);
|
||||
|
||||
String str = "Die Katze jagt die Maus";
|
||||
Stream<String> wörterString = Arrays.stream(str.split(" "));
|
||||
|
||||
|
||||
|
||||
|
||||
List<String> saetze = Arrays.asList(
|
||||
"Die Katze jagt die Maus",
|
||||
"ist klein",
|
||||
"Der Hund ",
|
||||
"Die Katze schläft Bellt"
|
||||
);
|
||||
|
||||
//finde das kleinste Wort
|
||||
|
||||
String maxWort = saetze.stream()
|
||||
.flatMap(satz -> Arrays.stream(satz.split(" "))) // Teilt jeden Satz in Wörter
|
||||
.max(String::compareTo) // finde das lexikografisch größte Wort
|
||||
.get();
|
||||
|
||||
// System.out.println("maxWort " + maxWort);
|
||||
|
||||
// speichere jede Wort der nur 5 Zeichen enthalten und alle Kleinbuchstaben
|
||||
List<String> mindestensfünfZeichen = saetze.stream()
|
||||
.flatMap(satz -> Arrays.stream(satz.split(" ")))
|
||||
.filter(wort -> wort.length() == 5)
|
||||
.map(wort -> wort.toLowerCase())
|
||||
.toList();
|
||||
|
||||
//System.out.println(mindestensfünfZeichen);
|
||||
|
||||
|
||||
// saetze.stream()
|
||||
// .flatMap(satz -> Arrays.stream(satz.split(" ")))
|
||||
// .forEach(wort -> System.out.println("Wort: " + wort + " Wortlänge " + wort.length()));
|
||||
//
|
||||
//
|
||||
// Stream<Double> stream = Stream.of(3.0, 2.0);
|
||||
//
|
||||
// stream
|
||||
// .forEach(i -> {
|
||||
// double summe = i.reduce(0.0, (i,j) -> i+j);
|
||||
// });
|
||||
//
|
||||
//
|
||||
// System.out.println(z);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
package streams;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class CollectMethode {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] array = {3,7,1,8,4,5,5,3,4,1};
|
||||
|
||||
Map<Integer,List<Integer>> map = IntStream.of(array)
|
||||
.boxed()
|
||||
.collect(Collectors.groupingBy(Function.identity()));
|
||||
|
||||
map.keySet().stream().forEach(System.out::println);
|
||||
|
||||
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
|
||||
|
||||
// Variante 1
|
||||
List<Integer> zahlen = numbers.stream()
|
||||
.map(z -> z * z)
|
||||
.collect(Collectors.toCollection(ArrayList::new));
|
||||
|
||||
// zahlen.forEach(System.out::println);
|
||||
|
||||
String result = numbers.stream()
|
||||
.map(z -> z *z)
|
||||
.map(z -> z +"")
|
||||
.collect(Collectors.joining(",","[", "]"));
|
||||
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
}
|
Binary file not shown.
Loading…
Reference in New Issue