SWE Mid-term Practical Quiz
countries.stream().
map(Country::name).
forEach(System.out::println);
countries.stream().
map(Country::capital).
sorted(Comparator.nullsFirst(Comparator.naturalOrder())).
forEach(System.out::println);
countries.stream().
map(Country::capital).
sorted(Comparator.nullsLast(Comparator.reverseOrder())).
forEach(System.out::println);
countries.stream().
mapToLong(Country::population).
max().
getAsLong();
countries.stream().
mapToLong(Country::population).
average().
getAsDouble();
countries.stream().
mapToLong(Country::population).
summaryStatistics();
countries.stream().
filter(country -> country.region() == Region.EUROPE).
map(Country::name).
forEach(System.out::println);
countries.stream().
filter(country -> country.region() == Region.EUROPE).
map(Country::name).
count();
countries.stream().
filter(country -> country.independent()).
count();
countries.stream().
filter(country -> country.population() < 100).
forEach(System.out::println);
countries.stream().
filter(country -> country.population() < 100).
map(Country::name).
forEach(System.out::println);
countries.stream().
filter(country -> country.region() == Region.EUROPE).
mapToLong(Country::population).
sum();
countries.stream().
filter(country -> country.region() == Region.EUROPE).
mapToLong(Country::population).
sorted().
forEach(System.out::println);
countries.stream().
filter(country -> country.region() == Region.EUROPE).
mapToLong(Country::population).
boxed().
sorted(Comparator.reverseOrder()).
forEach(System.out::println);
Country mostPopulousCountry = countries.stream().
filter(country -> country.region() == Region.EUROPE).
max(Comparator.comparingLong(Country::population)).
get();
countries.stream().
filter(country -> country.region() == Region.EUROPE).
max(Comparator.comparingLong(Country::population)).
get().
name();
countries.stream().
map(Country::name).
limit(5).
forEach(System.out::println);
countries.stream().anyMatch(country -> country.population() == 0);
countries.stream().allMatch(country -> !country.timezones().isEmpty());
countries.stream().
filter(country -> country.name().charAt(0) == 'H').
findFirst();
long numberOfTimezones = countries.stream().
flatMap(country -> country.timezones().stream()).
distinct().
count();
countries.stream().
filter(country -> country.region() == Region.EUROPE).
flatMap(country -> country.timezones().stream()).
distinct().
forEach(System.out::println);
countries.stream().
sorted(Comparator.comparingLong(Country::population)).
forEach(country -> System.out.printf("%s: %d\n", country.name(), country.population()));
countries.stream().
map(Country::name).
max(Comparator.comparingInt(String::length)).
get();
countries.stream().
map(Country::capital).
sorted(Comparator.nullsFirst(Comparator.comparingInt(String::length))).
forEach(System.out::println);
countries.stream().
map(Country::capital).
sorted(Comparator.nullsFirst(Comparator.comparingInt(String::length).thenComparing(Comparator.naturalOrder()))).
forEach(System.out::println);
countries.stream().anyMatch(country -> country.name().toLowerCase().contains("island"));
countries.stream()
.map(Country::name)
.filter(s -> s.toLowerCase().contains("island"))
.findFirst()
.get();
countries.stream()
.map(Country::name)
.filter(s -> { String t = s.toLowerCase(); return t.charAt(0) == t.charAt(t.length() - 1); })
.forEach(System.out::println);
countries.stream()
.mapToLong(Country::population)
.sorted()
.limit(10)
.forEach(System.out::println);
countries.stream()
.sorted(Comparator.comparingLong(Country::population))
.limit(10)
.map(Country::name)
.forEach(System.out::println);
countries.stream()
.mapToInt(country -> country.translations().size())
.summaryStatistics();
countries.stream()
.sorted(Comparator.comparingInt(country -> country.timezones().size()))
.map(Country::name)
.forEach(System.out::println);
countries.stream()
.sorted(Comparator.comparingInt(country -> country.timezones().size()))
.forEach(country -> System.out.printf("%s:%d\n", country.name(), country.timezones().size()));
countries.stream()
.filter(country -> !country.translations().containsKey("fa"))
.count();
countries.stream()
.filter(country -> country.area() == null)
.map(Country::name)
.forEach(System.out::println);
countries.stream()
.flatMap(country -> country.translations().keySet().stream())
.sorted()
.distinct()
.forEach(System.out::println);
countries.stream()
.map(Country::name)
.mapToInt(String::length)
.average()
.getAsDouble();
countries.stream()
.filter(country -> country.area() == null)
.map(Country::region)
.distinct()
.forEach(System.out::println);
countries.stream()
.filter(country -> country.area() != null)
.max(Comparator.comparing(Country::area));
countries.stream()
.filter(country -> country.area() != null && country.area().compareTo(BigDecimal.ONE) < 0)
.map(Country::name)
.forEach(System.out::println);
countries.stream()
.filter(country -> country.region() == Region.EUROPE || country.region() == Region.ASIA)
.flatMap(country -> country.timezones().stream())
.distinct()
.forEach(System.out::println);