Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

LinkedList accepting null values while ArrayDeque does not  RSS feed

 
Ranch Hand
Posts: 66
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: 61715
193
  • 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.
 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
Download Free Java APIs to Work with Office Files and PDF
htttp://www.e-iceblue.com/free-apis.html
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!