Page 1 of 1

About Question enthuware.ocpjp.v17.2.3732 :

Posted: Sun Aug 14, 2022 5:47 am
by ftejada
Hi,

Despite the Arrays.stream()'s Java doc not explicitly mentioning "sequential ordered", aren't arrays intrinsically ordered? According to the Stream ordering section: https://docs.oracle.com/en/java/javase/ ... l#Ordering

, a stream ordering is dictated by the "encounter order", in our case, defined by the source operation Arrays.stream(Card.values()), meaning that the stream will actually be ordered, thus takeWhile being deterministic.

Regardless of my statement, would any of the below options have ensured to print HEART ?

Code: Select all

1. Arrays.stream(Card.values())
       .sorted()
       .takeWhile(Card::isRed)
       .forEach(System.out::print);

Code: Select all

2. Stream.of(Card.values())
       .takeWhile(Card::isRed)
       .forEach(System.out::print);
Thank you

Re: About Question enthuware.ocpjp.v17.2.3732 :

Posted: Sun Aug 14, 2022 8:47 am
by admin
Yes, I think your point that arrays are intrinsically ordered is valid and the question and its explanation should be updated.

1. sorted should print HEART because, you are explicitly returning a sorted stream. All enums are Comparable (Enum class implements Comparable) and the natural sorting of enums is based on the order in which they are declared.

2. Your second code snippet will also print HEART because Stream.of returns an ordered stream order is same as encounter order).

thank you for your feedback!