I'm currently going through the Oracle tutorials - http://docs.oracle.com/javase/tutorial/collections/interfaces/list.html and as I read these tutorial, there are a couple of things that I find a little confusing.
One of the things that I don't understand is what do we mean when we say a collection implementation supports null values or doesn't support null values. I know, in general, Lists can have null values, a Set cannot have null values, and a Map can have one null key and several null values. But still I'm not sure about it.
Does an ArrayList support nulls?
I can add null values in an ArrayList<String> but I cannot use Collections.sort() method if it has nulls but contains() method would behave correctly. I could write a compare/compareTo method that could behave in a certain way with null values. Generally we wouldn't allow our Collection ( even more so, if the collection is a sorted collection implementation ) to contain nulls, but what do we exactly mean when we say that a collection can/cannot handle null values.
Would the following be true.
An implementation supports null values, if it can contain nulls, and such nulls are searchable? If a collection can contain null values, I guess we can't ever sort it. Right? Sorted collections hence can't contain nulls and these are the collection implementations that don't support nulls?
Another thing I don't really understand is the optional methods. If we have an implementation that we refer to as a concrete implementation ( a non abstract class ), shouldn't it( or its class hierarchy ) implement all the interface methods either directly or indirectly? I couldn't notice any optional List method that ArrayList class has not implemented. But am I missing something here?
The tutorial has the following text.
Here is a little text from the Oracle docs-
The Arrays class has a static factory method called asList, which allows an array to be viewed as a List. This method does not copy the array. Changes in the List write through to the array and vice versa. The resulting List is not a general-purpose List implementation, because it doesn't implement the (optional) add and remove operations: Arrays are not resizable.
So would it be correct to say that optional methods are for only those kind of interface implementations that cannot be directly instantiated but can be returned by a method ( or may be passed as an argument - perhaps the returned value only cause there'd be no other way to otherwise instantiate them).
Thanks,
Chan.