[Logo]
Forums Register Login
OCP Chapter 3 - remove method in Queue and remove method in List
Hello everyone,

in OCP page 168, there is the following question

               

The explanation on page 555 of the book says that:

LinkedList implements both List and Queue. The List interface has a method to
remove by index. Since this method exists, Java does not autobox to call the other method.
Queue has only the remove by object method, so Java does autobox there. Since the number
1 is not in the list, Java does not remove anything for the Queue



I did not understand the part  :

Queue has only the remove by object method



I searched the API of Queue in the following link:
https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html#remove--

and I only found this remove method:

remove

E remove()


What does OCP mean when saying that Queue has only the remove by object method?


Thanks,
Ioanna
Retrieves and removes the head of this queue. This method differs from poll only in that it throws an exception if this queue is empty.

 
If you look in the API for Queue, you see first the remove() with no arguments that you mentioned.  However, if you look below at the methods inherited by Collections, you will see a method with this signature:

boolean remove‚Äč(Object o)

This is the remove by Object the book is speaking of.
(2 likes)

This was careless naming IMO.  The Java Collections should never have sanctioned a (List).remove(int) to remove at a specific slot within the List.

It should have been named "removeAt(int)" or similar.

This probably happened because Java Collections far predated any consideration of Auto Boxing/Unboxing (Java 5 I believe), so it never occurred to anyone that an "intention" conflict might happen.

hello everyone and thank you for your answers !!!

In the first part of the code



How do we recognize that it uses E remove(int index) method from List interface instead of boolean remove(Object o) ???


Thanks,
Ioanna
 

How do we recognize


Read the API doc
(1 like)
 

How do we recognize that it uses E remove(int index) method from List interface instead of boolean remove(Object o) ???


In the code q.remove(1), "1" is an integer literal.  That means its type is int.  Since int is a Java primitive and not an Object, the JVM will use E remove(int index).

This is a case of method overloading, that is, the methods have the same name but not the same arguments.  Which method is called is determined by the type and number of arguments.
It was easier in the days of Java1.4; you didn't have boxing to contend with. If you called remove(999); in Java1.4, it counted as an int and in Java 8 it is still an int. You can't allow the behaviour of that sort of thing to change.
The OP asked:

How do we recognize that it uses


How can that be determined other than by reading the API doc?

If the method is documented as taking an int, then the int is used.
If the method does not take an int, the it will be boxed into an object.
You didn't tell me he was so big. Unlike this tiny ad:
The WEB SERVICES and JAX-RS Course
https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS


This thread has been viewed 122 times.

All times above are in ranch (not your local) time.
The current ranch time is
Feb 21, 2018 00:36:30.