Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can we add null values to LinkedList DESPITE strong discouragement of doing so in Deque interface?

From the Javadoc for Deque:

While Deque implementations are not strictly required to prohibit the insertion of null elements, they are strongly encouraged to do so. Users of any Deque implementations that do allow null elements are strongly encouraged not to take advantage of the ability to insert nulls. This is so because null is used as a special return value by various methods to indicated that the deque is empty.

I didn't find the answer in the existing question: Why can we add null elements to a java LinkedList?

like image 699
frostman Avatar asked Oct 18 '22 06:10

frostman


1 Answers

Such provisions are allowed to make programmers life easier. I can think of an example.

Suppose there is a linkedList of houses and a postMan comes to deliver letters to each house in sequence. Each Node in linkedList present house. Houses have a family object as their value. In some cases family may not be there then house object will have null value.

But for postman it is not relevant as he still has to visit that node. PostOffice has the suggestion to all families to indicate postoffice if they are leaving the house so that postMan need not to visit that house(if possible).

But for some purpose house owner decide to not inform postOffice about this situation and wish to continue receiving letters(indirectly a postman visit).

like image 195
Gaurava Agarwal Avatar answered Nov 10 '22 00:11

Gaurava Agarwal