Praktikum 3: Streams vllt
This commit is contained in:
parent
533e2a5962
commit
5b5325fc29
@ -1,20 +1,22 @@
|
|||||||
package de.joethei.hs.java2.praktikum.praktikum3;
|
package de.joethei.hs.java2.praktikum.praktikum3;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class Vorlesungsverzeichnis {
|
public class Vorlesungsverzeichnis implements Comparator<Vorlesung>{
|
||||||
private Set<Vorlesung> vorlesungen = new HashSet<>();
|
private Set<Vorlesung> vorlesungen = new HashSet<>();
|
||||||
|
|
||||||
public Vorlesungsverzeichnis(String filename) throws IOException {
|
public Vorlesungsverzeichnis(String filename) throws IOException {
|
||||||
|
|
||||||
List<List<String>> datenbasis;
|
List<List<String>> datenbasis;
|
||||||
datenbasis = load(filename);
|
datenbasis = load(filename);
|
||||||
for (List<String> a : datenbasis) {
|
for (List<String> a : datenbasis) {
|
||||||
if (a.size() != 4) throw new TextFileFormatException("unexpected number of Strings in line");
|
if (a.size() != 4) throw new TextFileFormatException("unexpected number of Strings in line");
|
||||||
for (String s : a) if (s.isEmpty()) throw new TextFileFormatException("empty attribute");
|
for (String s : a) if (s.isEmpty()) throw new TextFileFormatException("empty attribute");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
vorlesungen.add(new Vorlesung(a));
|
vorlesungen.add(new Vorlesung(a));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -29,15 +31,23 @@ public class Vorlesungsverzeichnis {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public List<String> titles() {
|
public List<String> titles() {
|
||||||
|
/*
|
||||||
List<String> list = new ArrayList<>();
|
List<String> list = new ArrayList<>();
|
||||||
for(Vorlesung v: this.vorlesungen) {
|
for(Vorlesung v: this.vorlesungen) {
|
||||||
list.add(v.getTitle());
|
list.add(v.getTitle());
|
||||||
}
|
}
|
||||||
Collections.sort(list);
|
Collections.sort(list);
|
||||||
return list;
|
return list;
|
||||||
|
|
||||||
|
*/
|
||||||
|
List<String> list = new ArrayList<>();
|
||||||
|
vorlesungen.forEach(vorlesung -> list.add(vorlesung.getTitle()));
|
||||||
|
Collections.sort(list);
|
||||||
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<String> workaholics() {
|
public Set<String> workaholics() {
|
||||||
|
/*
|
||||||
Set<String> s = new HashSet<>();
|
Set<String> s = new HashSet<>();
|
||||||
List<String> list = new ArrayList<>();
|
List<String> list = new ArrayList<>();
|
||||||
for(Vorlesung v:vorlesungen) {
|
for(Vorlesung v:vorlesungen) {
|
||||||
@ -49,9 +59,19 @@ public class Vorlesungsverzeichnis {
|
|||||||
}
|
}
|
||||||
Collections.sort(list);
|
Collections.sort(list);
|
||||||
return new HashSet<>(list);
|
return new HashSet<>(list);
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
Map<String, Set<String>> map = vorlesungen.stream().collect(Collectors.groupingBy(Vorlesung::getDozent, Collectors.mapping(Vorlesung::getTitle, Collectors.toSet())));
|
||||||
|
Set<String> set = new HashSet<>();
|
||||||
|
map.forEach((s, strings) -> {
|
||||||
|
if(strings.size() >= 2) set.add(s);
|
||||||
|
});
|
||||||
|
return set;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, List<String>> groupToTitles() {
|
public Map<String, List<String>> groupToTitles() {
|
||||||
|
/*
|
||||||
Map<String, List<String>> map = new HashMap<>();
|
Map<String, List<String>> map = new HashMap<>();
|
||||||
List<String> list = new ArrayList<>();
|
List<String> list = new ArrayList<>();
|
||||||
Set<String> s = new HashSet<>();
|
Set<String> s = new HashSet<>();
|
||||||
@ -69,9 +89,14 @@ public class Vorlesungsverzeichnis {
|
|||||||
list = new ArrayList<>();
|
list = new ArrayList<>();
|
||||||
}
|
}
|
||||||
return map;
|
return map;
|
||||||
|
*/
|
||||||
|
Map<String, List<String>> map = vorlesungen.stream().collect(Collectors.groupingBy(Vorlesung::getStudiengruppe, Collectors.mapping(Vorlesung::getTitle, Collectors.toList())));
|
||||||
|
map.forEach((s, strings) -> Collections.sort(strings));
|
||||||
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, List<String>> multipleTitles() {
|
public Map<String, List<String>> multipleTitles() {
|
||||||
|
/*
|
||||||
Map<String, List<String>> map = new HashMap<>();
|
Map<String, List<String>> map = new HashMap<>();
|
||||||
List<String> list = new ArrayList<>();
|
List<String> list = new ArrayList<>();
|
||||||
Set<String> s = new HashSet<>();
|
Set<String> s = new HashSet<>();
|
||||||
@ -93,9 +118,17 @@ public class Vorlesungsverzeichnis {
|
|||||||
if (map.get(title).size() < 2) map.remove(title);
|
if (map.get(title).size() < 2) map.remove(title);
|
||||||
}
|
}
|
||||||
return map;
|
return map;
|
||||||
|
*/
|
||||||
|
Map<String, List<String>> map = vorlesungen.stream().collect(Collectors.groupingBy(Vorlesung::getTitle, Collectors.mapping(Vorlesung::getDozent, Collectors.toList())));
|
||||||
|
map.forEach((s, strings) -> Collections.sort(strings));
|
||||||
|
|
||||||
|
|
||||||
|
return map;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> descendingTitles() {
|
public List<String> descendingTitles() {
|
||||||
|
/*
|
||||||
List<String> l = new ArrayList<>();
|
List<String> l = new ArrayList<>();
|
||||||
List<Vorlesung> vl = new ArrayList<>(vorlesungen);
|
List<Vorlesung> vl = new ArrayList<>(vorlesungen);
|
||||||
int most=0, index=0;
|
int most=0, index=0;
|
||||||
@ -111,5 +144,19 @@ public class Vorlesungsverzeichnis {
|
|||||||
vl.remove(index);
|
vl.remove(index);
|
||||||
}
|
}
|
||||||
return l;
|
return l;
|
||||||
|
|
||||||
|
*/
|
||||||
|
SortedSet<Vorlesung> set = new TreeSet<>(this.reversed());
|
||||||
|
set.addAll(vorlesungen);
|
||||||
|
|
||||||
|
List<String> list = new ArrayList<>();
|
||||||
|
set.forEach(vorlesung -> list.add(vorlesung.getTitle()));
|
||||||
|
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compare(Vorlesung o1, Vorlesung o2) {
|
||||||
|
return Integer.compare(o1.getTeilnehmerzahl(), o2.getTeilnehmerzahl());
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -81,8 +81,8 @@ public class VorlesungsverzeichnisTest {
|
|||||||
Map<String, List<String>> map = vorlesungsverzeichnis.multipleTitles();
|
Map<String, List<String>> map = vorlesungsverzeichnis.multipleTitles();
|
||||||
|
|
||||||
List<String> math = new ArrayList<>();
|
List<String> math = new ArrayList<>();
|
||||||
math.add("von Coelln");
|
|
||||||
math.add("Rabe");
|
math.add("Rabe");
|
||||||
|
math.add("von Coelln");
|
||||||
|
|
||||||
Collections.sort(math);
|
Collections.sort(math);
|
||||||
assertEquals(map.get("Mathematik 2"), math);
|
assertEquals(map.get("Mathematik 2"), math);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user