Übungen
parent
e2f05fe419
commit
429f82a672
|
@ -36,6 +36,10 @@ public class Main {
|
||||||
|
|
||||||
//Alle Prüfungen mit ihrem Namen, dem Semester, der Note und Namen des Studis ausgeben
|
//Alle Prüfungen mit ihrem Namen, dem Semester, der Note und Namen des Studis ausgeben
|
||||||
|
|
||||||
|
studenten.stream()
|
||||||
|
.forEach(st -> System.out.println(st.getName() +" ,Prüfungen: "+ st.getPrüfungen()));
|
||||||
|
System.out.println("Oben:");
|
||||||
|
System.out.println();
|
||||||
Map<Student,List<Prüfung>> aufgabe1 = studenten.stream()
|
Map<Student,List<Prüfung>> aufgabe1 = studenten.stream()
|
||||||
.collect(Collectors.toMap(st -> st,st -> st.getPrüfungen()));
|
.collect(Collectors.toMap(st -> st,st -> st.getPrüfungen()));
|
||||||
//aufgabe1.forEach((a,b) ->System.out.println(a + " " + b));
|
//aufgabe1.forEach((a,b) ->System.out.println(a + " " + b));
|
||||||
|
|
|
@ -7,7 +7,6 @@ import static java.util.stream.Collectors.groupingBy;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import com.example.domain.Movie;
|
|
||||||
|
|
||||||
|
|
||||||
public class Movie {
|
public class Movie {
|
||||||
|
|
|
@ -0,0 +1,67 @@
|
||||||
|
package Testate.Stream;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class Person {
|
||||||
|
private String name;
|
||||||
|
private int age;
|
||||||
|
private String city;
|
||||||
|
|
||||||
|
// Konstruktor
|
||||||
|
public Person(String name, int age, String city) {
|
||||||
|
this.name = name;
|
||||||
|
this.age = age;
|
||||||
|
this.city = city;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Getter-Methoden
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCity() {
|
||||||
|
return city;
|
||||||
|
}
|
||||||
|
|
||||||
|
// toString-Methode für eine sinnvolle Ausgabe
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Person{" +
|
||||||
|
"name='" + name + '\'' +
|
||||||
|
", age=" + age +
|
||||||
|
", city='" + city + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// Erstellen der Personenliste
|
||||||
|
List<Person> persons = new ArrayList<>();
|
||||||
|
persons.add(new Person("Alice", 25, "Berlin"));
|
||||||
|
persons.add(new Person("Bob", 35, "Hamburg"));
|
||||||
|
persons.add(new Person("Charlie", 40, "München"));
|
||||||
|
persons.add(new Person("Diana", 28, "Berlin"));
|
||||||
|
persons.add(new Person("Eve", 32, "Köln"));
|
||||||
|
persons.add(new Person("Anna", 30, "Berlin"));
|
||||||
|
|
||||||
|
Comparator<Person> com = (a,b) -> a.getName().compareTo(b.getName());
|
||||||
|
|
||||||
|
persons.stream()
|
||||||
|
.filter(p -> p.getCity().equals("Berlin"))
|
||||||
|
.filter(p -> p.getAge() >= 30)
|
||||||
|
.sorted(com)
|
||||||
|
.forEach(p -> System.out.println(p.toString()));
|
||||||
|
|
||||||
|
persons.stream()
|
||||||
|
.mapToDouble(p -> p.getAge())
|
||||||
|
.average()
|
||||||
|
.orElse(0.0);
|
||||||
|
|
||||||
|
persons.stream()
|
||||||
|
.collect(Collectors.averagingLong(null));
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,57 +0,0 @@
|
||||||
package Testate.Stream;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
public class SortAlgorithm {
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
int[] arr = {60,40,50,30,10,20};
|
|
||||||
SelectionSort(arr);
|
|
||||||
|
|
||||||
for (int i : arr)
|
|
||||||
System.out.println(i);
|
|
||||||
|
|
||||||
ArrayList<Integer> array = new ArrayList<>();
|
|
||||||
array.add(10);
|
|
||||||
array.add(4);
|
|
||||||
array.add(5);
|
|
||||||
array.add(6);
|
|
||||||
SelectionSort2(array);
|
|
||||||
System.out.println(array);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static void SelectionSort(int[] array) {
|
|
||||||
int n = array.length;
|
|
||||||
for (int i = 0; i < n - 1; i++) {
|
|
||||||
int minIndex = i;
|
|
||||||
for (int j = i + 1; j < n; j++) {
|
|
||||||
if (array[j] > array[minIndex]) {
|
|
||||||
minIndex = j;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Tausche das kleinste Element mit dem ersten Element des unsortierten Teils
|
|
||||||
int temp = array[minIndex];
|
|
||||||
array[minIndex] = array[i];
|
|
||||||
array[i] = temp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void SelectionSort2(ArrayList<Integer> array) {
|
|
||||||
int n = array.size();
|
|
||||||
for (int i = 0; i < n - 1; i++) {
|
|
||||||
int minIndex = i;
|
|
||||||
for (int j = i + 1; j < n; j++) {
|
|
||||||
if (array.get(j).compareTo(array.get(minIndex)) > 0 ) {
|
|
||||||
minIndex = j;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Tausche das kleinste Element mit dem ersten Element des unsortierten Teils
|
|
||||||
int temp = array.get(minIndex);
|
|
||||||
array.add(array.get(i));
|
|
||||||
array.add(temp);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,53 @@
|
||||||
|
package Testate.Stream.SortAlgorithm;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class Person implements Comparable<Person> {
|
||||||
|
|
||||||
|
String name;
|
||||||
|
int alter;
|
||||||
|
|
||||||
|
public Person(String name, int alter) {
|
||||||
|
super();
|
||||||
|
this.name = name;
|
||||||
|
this.alter = alter;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Person [name=" + name + ", alter=" + alter + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compareTo(Person p) {
|
||||||
|
if (this.alter > p.alter) // Aufsteigend
|
||||||
|
return 1;
|
||||||
|
else
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Person[] personen = {new Person("obai",14),new Person("Ahmad",20), new Person("Abd",12)};
|
||||||
|
bubbleSort(personen);
|
||||||
|
for (Person p: personen)
|
||||||
|
System.out.println(p.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void bubbleSort(Person[] p) {
|
||||||
|
boolean tauche;
|
||||||
|
for (int i = 0; i < p.length - 1; i++) {
|
||||||
|
tauche = false;
|
||||||
|
for (int j = 0; j < p.length-1; j++)
|
||||||
|
if (p[j].compareTo(p[j + 1]) > 0) { // >0: Aufsteigend sortieren
|
||||||
|
Person temp = p[j];
|
||||||
|
p[j] = p[j + 1];
|
||||||
|
p[j + 1] = temp;
|
||||||
|
tauche = true;
|
||||||
|
}
|
||||||
|
if (!tauche)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,67 @@
|
||||||
|
package Testate.Stream.SortAlgorithm;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class SortAlgorthm {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
int[] zahlen = {10,5,11,3};
|
||||||
|
TreeSet<Person> ts = new TreeSet<>();
|
||||||
|
ts.add(new Person("obai",12));
|
||||||
|
for(Person p : ts)
|
||||||
|
System.out.println(p.toString());
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void insertionSort(int[] zahlen) {
|
||||||
|
|
||||||
|
for (int i = 1; i < zahlen.length; i++) {
|
||||||
|
|
||||||
|
int merker = zahlen[i];
|
||||||
|
int j = i - 1;
|
||||||
|
while(j >= 0 && zahlen[j] > merker) {
|
||||||
|
zahlen[j+1] = zahlen[j];
|
||||||
|
j--;
|
||||||
|
}
|
||||||
|
|
||||||
|
zahlen[j+1] = merker;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void selectionSort(int[] zahlen) {
|
||||||
|
for (int i = 0; i < zahlen.length; i++) {
|
||||||
|
|
||||||
|
int merker = i;
|
||||||
|
for (int j = i + 1; j < zahlen.length ; j++) {
|
||||||
|
if (zahlen[j] < zahlen[merker])
|
||||||
|
merker = j;
|
||||||
|
}
|
||||||
|
int temp = zahlen[merker];
|
||||||
|
zahlen[merker] = zahlen[i];
|
||||||
|
zahlen[i] = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static void bubbleSort(int[] zahlen) {
|
||||||
|
boolean tauch;
|
||||||
|
|
||||||
|
for (int i = 0; i < zahlen.length - 1; i++) {
|
||||||
|
tauch = false;
|
||||||
|
for (int j = 0; j < zahlen.length - 1; j++)
|
||||||
|
if (zahlen[j] > zahlen[j + 1]) {
|
||||||
|
int temp = zahlen[j];
|
||||||
|
zahlen[j] = zahlen[j + 1];
|
||||||
|
zahlen[j + 1] = temp;
|
||||||
|
tauch = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!tauch)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue