Page 1 of 1

About Question enthuware.ocpjp.v8.2.1171 :

Posted: Tue Mar 01, 2016 6:13 pm
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.

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

Posted: Tue Mar 01, 2016 7:33 pm
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.

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

Posted: Fri Jul 22, 2016 9:35 am
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.

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

Posted: Fri Jul 22, 2016 11:33 am
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.

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

Posted: Thu Jul 26, 2018 8:13 am
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".

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

Posted: Thu Jul 26, 2018 9:37 am
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.

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

Posted: Fri Aug 03, 2018 12:36 pm
by __JJ__
OK, thank you.