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:

  1. Let's create a new hashset with the existing elements s1 (which I consider to be another set ).

  2. The newly created code HashSet will be stored in the form of a set name as union - this code seems to be new set <

  3. for the union , any other element from the collection (probably add method now, I think The union object contains the union of set s s1 and s2 .

    >

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

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -