Praktikum 3: Streams vllt
This commit is contained in:
parent
533e2a5962
commit
5b5325fc29
|
@ -1,19 +1,21 @@
|
||||||
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));
|
||||||
}
|
}
|
||||||
|
@ -22,22 +24,30 @@ public class Vorlesungsverzeichnis {
|
||||||
private List<List<String>> load(String filename) throws IOException {
|
private List<List<String>> load(String filename) throws IOException {
|
||||||
List<List<String>> result = new ArrayList<>();
|
List<List<String>> result = new ArrayList<>();
|
||||||
BufferedReader br = new BufferedReader(new FileReader(filename));
|
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(":")));
|
result.add(Arrays.asList(line.split(":")));
|
||||||
br.close();
|
br.close();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
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,18 +59,28 @@ 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<>();
|
||||||
for(Vorlesung v:vorlesungen) {
|
for (Vorlesung v : vorlesungen) {
|
||||||
s.add(v.getStudiengruppe());
|
s.add(v.getStudiengruppe());
|
||||||
}
|
}
|
||||||
for(String gruppe : s) {
|
for (String gruppe : s) {
|
||||||
for(Vorlesung v : vorlesungen) {
|
for (Vorlesung v : vorlesungen) {
|
||||||
if(gruppe.equals(v.getStudiengruppe())) {
|
if (gruppe.equals(v.getStudiengruppe())) {
|
||||||
list.add(v.getTitle());
|
list.add(v.getTitle());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -69,18 +89,23 @@ 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<>();
|
||||||
for(Vorlesung v:vorlesungen) {
|
for (Vorlesung v : vorlesungen) {
|
||||||
s.add(v.getTitle());
|
s.add(v.getTitle());
|
||||||
}
|
}
|
||||||
for(String title : s) {
|
for (String title : s) {
|
||||||
for(Vorlesung v:vorlesungen) {
|
for (Vorlesung v : vorlesungen) {
|
||||||
if(title.equals(v.getTitle())) {
|
if (title.equals(v.getTitle())) {
|
||||||
list.add(v.getDozent());
|
list.add(v.getDozent());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -89,13 +114,21 @@ public class Vorlesungsverzeichnis {
|
||||||
list = new ArrayList<>();
|
list = new ArrayList<>();
|
||||||
}
|
}
|
||||||
List<String> l = new ArrayList<>(map.keySet());
|
List<String> l = new ArrayList<>(map.keySet());
|
||||||
for(String title:l) {
|
for (String title : l) {
|
||||||
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…
Reference in New Issue