SWE Mid-term Practical Quiz

SWE Mid-term Practical Quiz

// 1. Prints country names:
countries.stream().
  map(Country::name).
  forEach(System.out::println);

// 2. Prints the capital of each country in alphabetical order:
countries.stream().
  map(Country::capital).
  sorted(Comparator.nullsFirst(Comparator.naturalOrder())).
  forEach(System.out::println);

// 3. Prints the capital of each country in reverse alphabetical order:
countries.stream().
  map(Country::capital).
  sorted(Comparator.nullsLast(Comparator.reverseOrder())).
  forEach(System.out::println);

// 4. Returns the maximum population:
countries.stream().
  mapToLong(Country::population).
  max().
  getAsLong();

// 5. Returns population average:
countries.stream().
  mapToLong(Country::population).
  average().
  getAsDouble();

// 6. Returns summary statistics about the population field:
countries.stream().
  mapToLong(Country::population).
  summaryStatistics();

// 7. Prints the names of European countries:
countries.stream().
  filter(country -> country.region() == Region.EUROPE).
  map(Country::name).
  forEach(System.out::println);

// 8. Returns the number of European countries:
countries.stream().
  filter(country -> country.region() == Region.EUROPE).
  map(Country::name).
  count();

// 9. Returns the number of independent countries:
countries.stream().
  filter(country -> country.independent()).
  count();

// 10. Prints all countries with population below 100:
countries.stream().
  filter(country -> country.population() < 100).
  forEach(System.out::println);

// 11. Prints the names of countries with population below 100:
countries.stream().
  filter(country -> country.population() < 100).
  map(Country::name).
  forEach(System.out::println);

// 12. Returns the sum of population of European countries:
countries.stream().
  filter(country -> country.region() == Region.EUROPE).
  mapToLong(Country::population).
  sum();

// 13. Prints the population of European countries in ascending order:
countries.stream().
  filter(country -> country.region() == Region.EUROPE).
  mapToLong(Country::population).
  sorted().
  forEach(System.out::println);

// 14. Prints the population of European countries in descending order:
countries.stream().
  filter(country -> country.region() == Region.EUROPE).
  mapToLong(Country::population).
  boxed(). // returns a Stream of java.lang.Long objects (required because LongStream has only a no-argument sorted() operation)
  sorted(Comparator.reverseOrder()).
  forEach(System.out::println);

// 15. Returns the European country with the highest population:
Country mostPopulousCountry = countries.stream().
  filter(country -> country.region() == Region.EUROPE).
  max(Comparator.comparingLong(Country::population)).
  get();

// 16. Returns the name of the European country with the highest population:
countries.stream().
  filter(country -> country.region() == Region.EUROPE).
  max(Comparator.comparingLong(Country::population)).
  get().
  name();

// 17. Prints the names of the first five countries:
countries.stream().
  map(Country::name).
  limit(5).
  forEach(System.out::println);

// 18. Returns whether there is at least one country with 0 population:
countries.stream().anyMatch(country -> country.population() == 0);

// 19. Returns whether each country has at least one timezone:
countries.stream().allMatch(country -> !country.timezones().isEmpty());

// 20. Returns the first country whose name starts with ‘H’:
countries.stream().
  filter(country -> country.name().charAt(0) == 'H').
  findFirst();

// 21. Returns the number of all distinct timezones:
long numberOfTimezones = countries.stream().
  flatMap(country -> country.timezones().stream()).
  distinct().
  count();

// 22. Prints all distinct timezones of European countries:
countries.stream().
  filter(country -> country.region() == Region.EUROPE).
  flatMap(country -> country.timezones().stream()).
  distinct().
  forEach(System.out::println);

// 23. Prints the name and population of each country in descending order of population:
countries.stream().
  sorted(Comparator.comparingLong(Country::population)).
  forEach(country -> System.out.printf("%s: %d\n", country.name(), country.population()));

// 24. Returns the length of the longest country name:
countries.stream().
  map(Country::name).
  max(Comparator.comparingInt(String::length)).
  get();

// 25. Prints the capital of each country in ascending order of length:
countries.stream().
  map(Country::capital).
  sorted(Comparator.nullsFirst(Comparator.comparingInt(String::length))).
  forEach(System.out::println);

// 26. Prints the capital of each country in ascending order of length and then in alphabetical order:
countries.stream().
  map(Country::capital).
  sorted(Comparator.nullsFirst(Comparator.comparingInt(String::length).thenComparing(Comparator.naturalOrder()))).
  forEach(System.out::println);
// 1. Returns whether there is at least one country with the word "island" in its name ignoring case:
countries.stream().anyMatch(country -> country.name().toLowerCase().contains("island"));

// 2. Returns the first country name that contains the word "island" ignoring case:
countries.stream()
  .map(Country::name)
  .filter(s -> s.toLowerCase().contains("island"))
  .findFirst()
  .get();

// 3. Prints each country name in which the first and the last letters are the same ignoring case:
countries.stream()
  .map(Country::name)
  .filter(s -> { String t = s.toLowerCase(); return t.charAt(0) == t.charAt(t.length() - 1); })
  .forEach(System.out::println);

// 4. Prints the populations of the first ten least populous countries:
countries.stream()
  .mapToLong(Country::population)
  .sorted()
  .limit(10)
  .forEach(System.out::println);

// 5. Prints the names of the first ten least populous countries:
countries.stream()
  .sorted(Comparator.comparingLong(Country::population))
  .limit(10)
  .map(Country::name)
  .forEach(System.out::println);

// 6. Returns summary statistics about the number of country name translations associated with each country:
countries.stream()
  .mapToInt(country -> country.translations().size())
  .summaryStatistics();

// 7. Prints the names of countries in the ascending order of the number of timezones:
countries.stream()
  .sorted(Comparator.comparingInt(country -> country.timezones().size()))
  .map(Country::name)
  .forEach(System.out::println);

// 8. Prints the number of timezones for each country in the form name:timezones, in the ascending order of the number of timezones:
countries.stream()
  .sorted(Comparator.comparingInt(country -> country.timezones().size()))
  .forEach(country -> System.out.printf("%s:%d\n", country.name(), country.timezones().size()));

// 9. Returns the number of countries with no Farsi country name translation:
countries.stream()
  .filter(country -> !country.translations().containsKey("fa"))
  .count();

// 10. Prints the names of countries with null area:
countries.stream()
  .filter(country -> country.area() == null)
  .map(Country::name)
  .forEach(System.out::println);

// 11. Prints all distinct language tags of country name translations sorted in alphabetical order:
countries.stream()
  .flatMap(country -> country.translations().keySet().stream())
  .sorted()
  .distinct()
  .forEach(System.out::println);

// 12. Returns the average length of country names:
countries.stream()
  .map(Country::name)
  .mapToInt(String::length)
  .average()
  .getAsDouble();

// 13. Prints all distinct regions of the countries with null area:
countries.stream()
  .filter(country -> country.area() == null)
  .map(Country::region)
  .distinct()
  .forEach(System.out::println);

// 14. Returns the largest country with non-null area:
countries.stream()
  .filter(country -> country.area() != null)
  .max(Comparator.comparing(Country::area));

// 15. Prints the names of countries with a non-null area below 1:
countries.stream()
  .filter(country -> country.area() != null && country.area().compareTo(BigDecimal.ONE) < 0)
  .map(Country::name)
  .forEach(System.out::println);

// 16. Prints all distinct timezones of European and Asian countries:
countries.stream()
  .filter(country -> country.region() == Region.EUROPE || country.region() == Region.ASIA)
  .flatMap(country -> country.timezones().stream())
  .distinct()
  .forEach(System.out::println);

Did you find this article valuable?

Support Mojtaba Maleki by becoming a sponsor. Any amount is appreciated!