About Question enthuware.ocpjp.v17.2.3732 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ftejada
Posts: 5
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: 10036
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!
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 32 guests