Übungen
parent
429f82a672
commit
1094628075
|
@ -0,0 +1,154 @@
|
|||
package Testate.Stream;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.*;
|
||||
|
||||
public class Country {
|
||||
private String code;
|
||||
private String name;
|
||||
private String continent;
|
||||
private double surfaceArea;
|
||||
private int population;
|
||||
private double gnp;
|
||||
private int capital;
|
||||
private List<City> cities;
|
||||
{
|
||||
cities = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Country(String code, String name, String continent, int population, double surfaceArea, double gnp,
|
||||
int capital) {
|
||||
this.code = code;
|
||||
this.name = name;
|
||||
this.continent = continent;
|
||||
this.surfaceArea = surfaceArea;
|
||||
this.population = population;
|
||||
this.capital = capital;
|
||||
this.gnp = gnp;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getContinent() {
|
||||
return continent;
|
||||
}
|
||||
|
||||
public double getSurfaceArea() {
|
||||
return surfaceArea;
|
||||
}
|
||||
|
||||
public double getGnp() {
|
||||
return gnp;
|
||||
}
|
||||
|
||||
public int getCapital() {
|
||||
return capital;
|
||||
}
|
||||
|
||||
public void setPopulation(int population) {
|
||||
this.population = population;
|
||||
}
|
||||
|
||||
public List<City> getCities() {
|
||||
return cities;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Country [ name=" + name + ", population=" + population + "]";
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Städte
|
||||
City city1 = new City(1, "Berlin", "DE", 3769000);
|
||||
City city2 = new City(2, "Hamburg", "DE", 1841000);
|
||||
City city3 = new City(3, "Munich", "DE", 1472000);
|
||||
City city4 = new City(4, "Paris", "FR", 2161000);
|
||||
City city5 = new City(5, "Lyon", "FR", 513000);
|
||||
City city6 = new City(6, "Marseille", "FR", 861000);
|
||||
City city7 = new City(7, "Madrid", "ES", 3223000);
|
||||
City city8 = new City(8, "Barcelona", "ES", 1620000);
|
||||
City city9 = new City(9, "New York", "US", 8419000);
|
||||
City city10 = new City(10, "Los Angeles", "US", 3980000);
|
||||
|
||||
// Länder
|
||||
Country country1 = new Country("DE", "Germany", "Europe", 83100000, 357022, 38456.0, 1);
|
||||
country1.getCities().addAll(Arrays.asList(city1, city2, city3));
|
||||
|
||||
Country country2 = new Country("FR", "France", "Europe", 67390000, 551695, 27775.0, 4);
|
||||
country2.getCities().addAll(Arrays.asList(city4, city5, city6));
|
||||
|
||||
Country country3 = new Country("ES", "Spain", "Europe", 47350000, 505990, 12345.0, 7);
|
||||
country3.getCities().addAll(Arrays.asList(city7, city8));
|
||||
|
||||
Country country4 = new Country("US", "United States", "North America", 331000000, 9833517, 211375.0, 9);
|
||||
country4.getCities().addAll(Arrays.asList(city9, city10));
|
||||
|
||||
|
||||
|
||||
// Testliste für Länder
|
||||
List<Country> countries = new ArrayList<>();
|
||||
countries.add(country1);
|
||||
countries.add(country2);
|
||||
countries.add(country3);
|
||||
countries.add(country4);
|
||||
|
||||
|
||||
// Map<Country,List<City>> a2 = countries.stream()
|
||||
// .collect(Collectors.toMap(c -> c, ci -> ci.getCities()));
|
||||
|
||||
Map<Country,City> a2_2 = countries.stream()
|
||||
.collect(Collectors.toMap(c -> c,
|
||||
c -> c.getCities().stream()
|
||||
.max(Comparator.comparingInt(city -> city.getPopulation()))
|
||||
.orElse(null)
|
||||
));
|
||||
|
||||
a2_2.forEach((a,b) -> System.out.println( a + " " + b));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class City {
|
||||
private int id;
|
||||
private String name;
|
||||
private int population;
|
||||
private String countryCode;
|
||||
|
||||
public City(int id, String name, String countryCode, int population) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.population = population;
|
||||
this.countryCode = countryCode;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getPopulation() {
|
||||
return population;
|
||||
}
|
||||
|
||||
public String getCountryCode() {
|
||||
return countryCode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "City [id=" + id + ", name=" + name + ", population=" + population + ", countryCode=" + countryCode
|
||||
+ "]";
|
||||
};
|
||||
|
||||
}
|
|
@ -1,14 +1,7 @@
|
|||
package Testate.Stream;
|
||||
|
||||
import static java.util.Map.Entry.comparingByValue;
|
||||
import static java.util.stream.Collectors.counting;
|
||||
import static java.util.stream.Collectors.groupingBy;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
|
||||
public class Movie {
|
||||
private int id;
|
||||
private String title;
|
||||
|
@ -90,32 +83,12 @@ public class Movie {
|
|||
movies.add(movie1);
|
||||
movies.add(movie2);
|
||||
movies.add(movie3);
|
||||
//movies.forEach(System.out::println);
|
||||
|
||||
//Finde die Anzahl der Filme jedes Regisseurs.
|
||||
Map<String,Long> anzahl = movies.stream()
|
||||
.flatMap(s -> s.getDirectors().stream())
|
||||
.map(r -> r.getName())
|
||||
.collect(Collectors.groupingBy(name -> name, Collectors.counting()));
|
||||
//anzahl.forEach((a,b) -> System.out.println(a + " " +b));
|
||||
|
||||
//Finde die Liste der Filme, die nur die Genres "Drama" und "Komödie" haben.
|
||||
|
||||
List<Movie> aufgabe7 = movies.stream()
|
||||
.filter(f -> f.getGenres().stream().anyMatch(fil -> fil.getName().equalsIgnoreCase("Drama")))
|
||||
.collect(Collectors.toCollection(ArrayList::new));
|
||||
|
||||
// System.out.println(aufgabe7);
|
||||
|
||||
var maxMovieCountByYear =
|
||||
movies.stream()
|
||||
.collect(groupingBy(Movie::getYear,counting()))
|
||||
.entrySet()
|
||||
.stream()
|
||||
.max(comparingByValue());
|
||||
maxMovieCountByYear.ifPresent(System.out::println);
|
||||
|
||||
|
||||
//1. Finde die Anzahl der Filme jedes Regisseurs.
|
||||
Map<String,Long> a1 = movies.stream()
|
||||
.flatMap(m -> m.getDirectors().stream())
|
||||
.collect(Collectors.groupingBy(name -> name.getName(),Collectors.counting()));
|
||||
a1.forEach((a,b) -> System.out.println(a + " " + b));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue