How to implement List, Set, and Map in null free design?

Posted by Pyrolistical on Stack Overflow See other posts from Stack Overflow or by Pyrolistical
Published on 2009-07-02T03:51:13Z Indexed on 2010/04/07 8:33 UTC
Read the original article Hit count: 392

Filed under:
|
|
|

Its great when you can return a null/empty object in most cases to avoid nulls, but what about Collection like objects?

In Java, Map returns null if key in get(key) is not found in the map.

The best way I can think of to avoid nulls in this situation is to return an Entry<T> object, which is either the EmptyEntry<T>, or contains the value T.

Sure we avoid the null, but now you can have a class cast exception if you don't check if its an EmptyEntry<T>.

Is there a better way to avoid nulls in Map's get(K)?

And for argument sake, let's say this language don't even have null, so don't say just use nulls.

© Stack Overflow or respective owner

Related posts about architecture

Related posts about null