Praktikum 3: Streams vllt
This commit is contained in:
parent
533e2a5962
commit
5b5325fc29
|
@ -1,20 +1,22 @@
|
|||
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.stream.Collectors;
|
||||
|
||||
public class Vorlesungsverzeichnis {
|
||||
public class Vorlesungsverzeichnis implements Comparator<Vorlesung>{
|
||||
private Set<Vorlesung> vorlesungen = new HashSet<>();
|
||||
|
||||
public Vorlesungsverzeichnis(String filename) throws IOException {
|
||||
|
||||
List<List<String>> datenbasis;
|
||||
datenbasis = load(filename);
|
||||
for (List<String> a : datenbasis) {
|
||||
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");
|
||||
|
||||
|
||||
|
||||
vorlesungen.add(new Vorlesung(a));
|
||||
}
|
||||
}
|
||||
|
@ -29,15 +31,23 @@ public class Vorlesungsverzeichnis {
|
|||
}
|
||||
|
||||
public List<String> titles() {
|
||||
/*
|
||||
List<String> list = new ArrayList<>();
|
||||
for(Vorlesung v: this.vorlesungen) {
|
||||
list.add(v.getTitle());
|
||||
}
|
||||
Collections.sort(list);
|
||||
return list;
|
||||
|
||||
*/
|
||||
List<String> list = new ArrayList<>();
|
||||
vorlesungen.forEach(vorlesung -> list.add(vorlesung.getTitle()));
|
||||
Collections.sort(list);
|
||||
return list;
|
||||
}
|
||||
|
||||
public Set<String> workaholics() {
|
||||
/*
|
||||
Set<String> s = new HashSet<>();
|
||||
List<String> list = new ArrayList<>();
|
||||
for(Vorlesung v:vorlesungen) {
|
||||
|
@ -49,9 +59,19 @@ public class Vorlesungsverzeichnis {
|
|||
}
|
||||
Collections.sort(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() {
|
||||
/*
|
||||
Map<String, List<String>> map = new HashMap<>();
|
||||
List<String> list = new ArrayList<>();
|
||||
Set<String> s = new HashSet<>();
|
||||
|
@ -69,9 +89,14 @@ public class Vorlesungsverzeichnis {
|
|||
list = new ArrayList<>();
|
||||
}
|
||||
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() {
|
||||
/*
|
||||
Map<String, List<String>> map = new HashMap<>();
|
||||
List<String> list = new ArrayList<>();
|
||||
Set<String> s = new HashSet<>();
|
||||
|
@ -93,9 +118,17 @@ public class Vorlesungsverzeichnis {
|
|||
if (map.get(title).size() < 2) map.remove(title);
|
||||
}
|
||||
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() {
|
||||
/*
|
||||
List<String> l = new ArrayList<>();
|
||||
List<Vorlesung> vl = new ArrayList<>(vorlesungen);
|
||||
int most=0, index=0;
|
||||
|
@ -111,5 +144,19 @@ public class Vorlesungsverzeichnis {
|
|||
vl.remove(index);
|
||||
}
|
||||
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();
|
||||
|
||||
List<String> math = new ArrayList<>();
|
||||
math.add("von Coelln");
|
||||
math.add("Rabe");
|
||||
math.add("von Coelln");
|
||||
|
||||
Collections.sort(math);
|
||||
assertEquals(map.get("Mathematik 2"), math);
|
||||
|
|
Loading…
Reference in New Issue