java - Why we must copy one set before calling the appropriate bulk operation? -
Why do I have to write the first statement before performing a bulk operation ??
set & lt; Type & gt; Union = new hashset & lt; Type & gt; (S1); Union.addAll (s2);
The object must be urged before calling any object.
In this situation, trying to call the addAll
method on the null
object will cause NullPointerException
. Therefore, the set
union
should be made in any way, then it can be used in any meaningful way.
I think this will be a good idea to go to the given code and try to understand what the code is trying to accomplish.
set & lt; Type & gt; Union = new hashset & lt; Type & gt; (S1); Union.addAll (s2);
The above code tells me the following:
-
Let's create a new
hashset
with the existing elementss1
(which I consider to be anotherset
). -
The newly created code
HashSet
will be stored in the form of aset
name asunion
- this code seems to be newset <
for the union
, any otherelement from the collection
(probablyadd
method > now, I think Theunion
object contains the union ofset
ss1
ands2
.
While writing the code, someone should try to tell what the code wants to do. Explaining the code as to what it is doing, the result is getting more readable and the maintenance code is Others are going to be easy (and possibly an enjoyment) to read and understand.
Comments
Post a Comment