About Question enthuware.ocpjp.v17.2.3732 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
ftejada
Posts: 8
Joined: Mon Jan 28, 2019 2:00 pm
Contact:

About Question enthuware.ocpjp.v17.2.3732 :

Post 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

admin
Site Admin
Posts: 10384
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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!

Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests