• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

LinkedList accepting null values while ArrayDeque does not  RSS feed

 
Ranch Hand
Posts: 77
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question is regarding the fact that an ArrayDeque does not accept null values. The OCP study guide states the following: "You can’t put null in an ArrayDeque because methods like poll() use null as a special return value to indicate that the collection is empty. Since null has that meaning, Java forbids putting a null in there. That would just be confusing." I understand that but then why is it allowed for a LinkedList to accept null values. I would expect a LinkedList to reject null values as well because it also uses methods like poll()?

Can someone explain me this difference?


Thanks in advance

Bart
 
Marshal
Posts: 64086
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by going through the documentation for linked list and array deque. Also the Java™ Tutorials. Please tell us which guide (including authors and page number) you are quoting.
The two classes were designed about eight years apart. The ordinary linked list is intended as a List and Lists usually accept nulls. A linked list can also be used as a queue, but that is not its primary intent. ArrayDeque was designed as a Deque, not a list, so returning null from an empty queue would be confusing. And avoiding confusion is a good way to design an API.
 
Switching from electric heat to a rocket mass heater reduces your carbon footprint as much as parking 7 cars. Tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!