This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin 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
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

LinkedList accepting null values while ArrayDeque does not  RSS feed

 
Ranch Hand
Posts: 72
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: 62871
203
  • 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.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!