About Question enthuware.ocpjp.v8.2.1171 :

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

Moderator: admin

Post Reply
digitalillusion
Posts: 5
Joined: Wed Feb 25, 2015 4:11 pm
Contact:

About Question enthuware.ocpjp.v8.2.1171 :

Post by digitalillusion »

why queue cannot be accepted as a correct answer?
- it's an interface
- it is ordered
- it has no constraints on unicity of it's elements

Thanks in advance for the reply, and thanks for the great learning tool you offer.

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

Re: About Question enthuware.ocpjp.v8.2.1171 :

Post by admin »

The problem with Queue is that is does not impose the order of the elements as a part of its contract. The JavaDoc for Queue says:
Queues typically, but do not necessarily, order elements in a FIFO (first-in-first-out) manner.
, while List clearly says,
An ordered collection (also known as a sequence).
.

So if you are given a reference to a Queue object, you can never be sure about the order of elements that you retrieve from it. While in case of a List, you are sure that the elements in the List are in the order of insertion.

HTH,
Paul.

badbishop
Posts: 27
Joined: Fri Jul 22, 2016 9:14 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1171 :

Post by badbishop »

I think the question does not have a correct answer. List<E>, strictly speaking, doesn't fit the bill, as new members are not necessarily appendend to the end of the list. Consider this code snippet:

Code: Select all

        List<Integer> li = new ArrayList();
        li.add(0);
        li.add(1);
        li.add(0,2);
        System.out.println(li);
... that outputs

Code: Select all

[2, 0, 1]
Good point about the Queue<E> contract.

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

Re: About Question enthuware.ocpjp.v8.2.1171 :

Post by admin »

The question only asks for an interface that lets you meet the requirement, which is, "represent a collection having non-unique objects in the order of insertion". You can do that with a List. Yes, you can do other things with a List as well, such as inserting elements, but that is not relevant.

__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1171 :

Post by __JJ__ »

admin wrote:
Tue Mar 01, 2016 7:33 pm
The problem with Queue is that is does not impose the order of the elements as a part of its contract. The JavaDoc for Queue says:
Queues typically, but do not necessarily, order elements in a FIFO (first-in-first-out) manner.
, while List clearly says,
An ordered collection (also known as a sequence).
.

So if you are given a reference to a Queue object, you can never be sure about the order of elements that you retrieve from it. While in case of a List, you are sure that the elements in the List are in the order of insertion.

HTH,
Paul.

The question asks "Which interface would you use to represent a collection having non-unique objects in the order of insertion?"

Many millions of Queues must have been used to represent a collection having non-unique objects in the order of insertion since Java became a thing.
Just because some Queue implementations don't operate on FIFO basis doesn't mean a Queue can't be used to represent a collection having non-unique objects in the order of insertion. It can and it does, probably in the vast majority of cases (unknowable of course but the number will be high).
To put it another way, if I use a Q to do this I am not necessarily using the wrong collection.

The question needs to be rephrased if it is to mean, "which is the only interface which guarantees that it will maintain a collection of non-unique objects in order of insertion."

But yeah, it would just have been easier to answer "List".

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

Re: About Question enthuware.ocpjp.v8.2.1171 :

Post by admin »

It doesn't matter how many millions of queues have been used to represent a collection having non-unique objects in the order of insertion. We are talking about java.util.Queue and the JavaDoc description of java.util.Queue clearly says a Queue does not necessarily mean that it returns elements in insertion order. So your argument is incorrect. If you rely on a Queue to return elements in insertion order, your code is broken.

Also, java.util.Queue is an interface. That means, if you use a "Queue", you would really be using a class that implements Queue. JDK provides several Queue implementations. If the class that you use guarantees insertion order, then it most likely implements List as well (such as LinkedList, which implements Queue as well as List) and it guarantees insertion order on account of being a List and not a Queue.

Therefore, the question and the given answer are correct.

HTH,
Paul.

__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1171 :

Post by __JJ__ »

OK, thank you.

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests