why I have to write the first statement before calling bulk operation??
Set<Type> union = new HashSet<Type>(s1);
union.addAll(s2);
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
One must instantiate an object before calling a method.
In this case, trying to call the
addAllmethod on anullobject will cause aNullPointerException. Therefore, theSetunionobject must first be constructed somehow, then can it used in any meaningful way.I think it would be a good idea to go over the given code and try to understand what the code is trying to accomplish.
The above code is tells me the following:
Let’s make a new
HashSetfrom the existing elements of theCollections1(which I assume to be something like anotherSet.)The newly created
HashSetwill be stored as aSetwith the nameunion— the code seems to imply that this newSetis going to represent some kind of union.To the
union, elements from anotherCollection(probably anotherSetthat is calleds2) will be added by theaddAllmethod.Now, I think that the
unionobject contains an union of theSetss1ands2.When writing code, one should pay attention to trying to tell others what the code intends to do. Having the code explain what it’s doing is going to result in more readable and maintainable code that is going to make it easier (and possibly a pleasure) for others to read and understand.