Praktikum 3: Streams vllt

This commit is contained in:
Johannes Theiner 2018-04-22 19:53:00 +02:00
parent 533e2a5962
commit 5b5325fc29
2 changed files with 67 additions and 20 deletions

View File

@ -1,19 +1,21 @@
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");
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));
}
@ -22,22 +24,30 @@ public class Vorlesungsverzeichnis {
private List<List<String>> load(String filename) throws IOException {
List<List<String>> result = new ArrayList<>();
BufferedReader br = new BufferedReader(new FileReader(filename));
for (String line=br.readLine(); line!=null; line=br.readLine())
for (String line = br.readLine(); line != null; line = br.readLine())
result.add(Arrays.asList(line.split(":")));
br.close();
return result;
}
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,18 +59,28 @@ 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<>();
for(Vorlesung v:vorlesungen) {
for (Vorlesung v : vorlesungen) {
s.add(v.getStudiengruppe());
}
for(String gruppe : s) {
for(Vorlesung v : vorlesungen) {
if(gruppe.equals(v.getStudiengruppe())) {
for (String gruppe : s) {
for (Vorlesung v : vorlesungen) {
if (gruppe.equals(v.getStudiengruppe())) {
list.add(v.getTitle());
}
}
@ -69,18 +89,23 @@ 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<>();
for(Vorlesung v:vorlesungen) {
for (Vorlesung v : vorlesungen) {
s.add(v.getTitle());
}
for(String title : s) {
for(Vorlesung v:vorlesungen) {
if(title.equals(v.getTitle())) {
for (String title : s) {
for (Vorlesung v : vorlesungen) {
if (title.equals(v.getTitle())) {
list.add(v.getDozent());
}
}
@ -89,13 +114,21 @@ public class Vorlesungsverzeichnis {
list = new ArrayList<>();
}
List<String> l = new ArrayList<>(map.keySet());
for(String title:l) {
if(map.get(title).size() < 2) map.remove(title);
for (String title : l) {
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());
}
}

View File

@ -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);