OCP - 2

Functional Programming, Dates, Strings, Localization, Exceptions and Assertions

Functional Programming, Dates, Strings, Localization, Exceptions and Assertions


Kartei Details

Karten 115
Sprache English
Kategorie Informatik
Stufe Andere
Erstellt / Aktualisiert 22.02.2019 / 08.01.2020
Weblink
https://card2brain.ch/box/20190222_ocp_2
Einbinden
<iframe src="https://card2brain.ch/box/20190222_ocp_2/embed" width="780" height="150" scrolling="no" frameborder="0"></iframe>

What has to be considered when using the Collectors.toMap collector?

It does not allow duplicate keys when no merge function is defined

What does the CollectorspartitioningBy method do?

Partitioning is a special case of gruping that splits a list into two parts (hence the result is Map<Boolean, List<T>> or Map<Boolean, Set<T>> respectively)

What is the purpose of the Collectors.mapping function?

It is useful for multi-level reduction.

Map<City, Set<String>> lastNamesByCity = people.stream()
     .collect(
            groupingBy(Person::getCity,
               mapping(Person::getLastName, toSet())));

Is it problematic to call filter before limit on an infinte stream?

Yes, infinite loop!

What's the class that represents a Date and Time with a Timezone

ZonedDateTime

How can you manually construct a ZonedDateTime instance? Are there any pitfalls?

ZoneId zone = ZoneId.of("US/Easter");
ZonedDateTime zone = ZonedDateTime.of(2019, 1, 20, 6, 15, 30, 200, zone); # Last Parameter
 

There isn't an option to pass the Month-Enum!

How do you construct date/time instances?

Using static factories - their constructors are private!

What does Java Ouptut when using System.out.println to output date/time values?

All non-zero values (zero values are omitted)

What must be considered when using the plus___ and minus___ Methods on  LocalDate / LocalTime / LocalDateTime / ZonedDateTime?

Check for compatibility! Years, Months, Week and Days do not work on LocalTime and Hours, Minutes, Seconds and Nonos do not work on LocalDate!

What is the difference between Periods and Durations?

Periods are for large timespans of a day or more, whereas Durations are for smaller units of time.

How can you construct Periods? What has to be considered?

Using static factories, eg. Period.ofMonths(1)

The calls cannot be chained! Eg Period.ofMonths(1).ofDays(14) is equivalent to Period.ofDays(14)

What is the Output of the following and what does it mean?

System.out.println(Period.of(1,2,3))

P1Y2M3D

-> P for Period, 1 Year, 2 Months and 3 Days

Note that Weeks are not a concept on Period! A Period.ofWeeks(1) is equivalent to Period.ofDays(7)

What is the Output of the following and what does it mean?

System.out.println(Period.of(1,0,3))

P1Y3D

-> P for Period, 1 Year and 3 Days.

Note that Months are omitted!

What is the Output of the following and what does it mean?

System.out.println(Duration.ofDays(1))

PT24H

PT stands for Period of Time (=Duration). 24 Hours are printed but minutes and seconds are omitted (empty)

What is the Output of the following and what does it mean?

System.out.println(Duration.ofNanos(1))

PT0.000000001S

PT stands for Period of Time (=Duration). Nanos are represented as fractions of a second

What can the ChronoUnit enum be used for?

  • For constructing Durations, eg. Duration.of(1, ChronoUnits.Days)
  • For Calculating how far apart two temporal values are: ChronoUnit.Hours.between(alocalDate, anotherLocalDate)

What happens if you call ChronoUnit.MINUTES.between with a LocalDate and a LocalTime Object?

An exception sis thrown because you are not allowed to mix date and time objects

What happens here:

Duration days = Duration.ofDays(2)
LocalDate.of(2015, 5, 25).plus(days);

An exception is thrown! Use Period instead!

What is an instant? How can it be constructed?

An Instant represents a specific moment in time in the GMT time szone.

aZonedDateTime.toInstant();
Instant.now()

What has to be considered when calculating instants with the plus methods?

You can only add Days, Hours but no weeks and years.
eg. instant.plus(1, ChronoUnit.WEEKS) throws an exception

What happens with LocalDate right before and after a dailight saving change?

The timeZone offset changes, eg:

2016-03-13T01:30-05:00[US/Eastern]

2016-03-13T03:30-04:00[US/Eastern]

How can you get and set the sytem default locale?

Using Locale.getDefault and Locale.setDefault

How can you create a Locale object?

Using its constructor or the Locale.Builder Object. The latter is more tolerant when being inconsistent with capitalisation.

What's the naming convention for Resource Bundles?

BundlenName_language_locale.[properties|java]

How can you get a ResourceBundle instance and query the key X on it?

ResourceBundle rb = ResourceBundle.getBundle("Zoo", locale);
rb.getString("X");

How do you defined keys and values in property files?

Usually sing an equal sign (=) as a separator, but a colon (:) or space are also valid

How is this property file interpreted:

# a = b
! c = d
e =              f
g = h \
 ij
 

e = f (spaces at the beginning are ignored, not at the end!)
g = h \nij (Backslash for a line break. \n and \t can also be used)

What is the advanteage of using the Properties class?

You can provide default values when using the getProperty method, eg getProperty("key", "default)

What is a Java Class Bundle?

Instead of using a properties file, a Java class extending the LsitResourceBundle returns a set of Properties. The advantage over properties files are:

  • Types other than string
  • Create values of a property at runtime

How can you get an instance of a java class resource bundle?

The same as with a properties file resource bundle:

ResourceBundle.getBundle("BundleName", locale)

How does java determine which resource bundle to use?

  • Always look for the Java Class first, then the property file
  • Drop one thing at a time, if there are no matches, strting with the coutnry and then the language
  • Default locale and default resource bulde come last

How can you use the MessageFormat class?

Use the static method format(msg, params) and placeholders in the string: Hello, {0}

In which package are the classes for Number formatting?

java.text

How can you get an instance of a NumberFormat?

  • NumberFormat.getInstance() // Optional Param: locale
  • NumberFormat.getNumberInstance() // Optional Param: locale
    • Same as getInstance()
  • NumberFormat.getCurrencyInstance() // Optional Param: locale
  • NumberFormat.getPercentInstance() // Optional Param: locale

How can you use a NumberFormat instance?

Call the parse and format methods on it

Is NumberFormat Thread-Safe?

No

When does NumberFormat.parse stop and when does it throw an exception?

  • parses only the beginning of a string. Stops if not a valid number-element
  • An exception if the format is used incorrectly (eg. multiple commas) or does not start with a number (eg. a00)

How are Dates formatted?

Using the format method on date: date.format(DateTimeFormatter.ISO_LOCAL_DATE)

or

Using the format method on a DateTimeFormatter date: DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).format(aDate)

 

What must be considered when using date and time formatting?

Not everything is compatible, eg DateTimeFormatter.ofLocalizedTime cannot be used with a LocalDate

How can you construct a custom Date Format?

DateTimeFormatter.ofPattern("MMM dd, yyy, hh:mm")