This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collection

 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
previous post of mine is not printed.i don't know why.that's why i am again sending the same question.

Given two collection objects referenced by c1 and c2, which of these statements are true?

Select 2 correct options



(1) c1.retainAll(c2) will not modify c1
(2) c1.removeAll(c2) will not modify c1
(3) c1.addAll(c2) will return a new collection object, containing elements from both c1 and c2
(4) For: c2.retainAll(c1); c1.containsAll(c2); 2nd statement will return true
(5) For: c2.addAll(c1); c1.retainAll(c2); 2nd statement will have no practical effect on c1

the answers are 4 and 5.

Its a javabeat question.

I have no idea how to check all these options in a coding way since collection is a new topic for me.please help.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shiva Mohan:
previous post of mine is not printed.i don't know why.that's why i am again sending the same question.

Given two collection objects referenced by c1 and c2, which of these statements are true?

Select 2 correct options



(1) c1.retainAll(c2) will not modify c1
(2) c1.removeAll(c2) will not modify c1
(3) c1.addAll(c2) will return a new collection object, containing elements from both c1 and c2
(4) For: c2.retainAll(c1); c1.containsAll(c2); 2nd statement will return true
(5) For: c2.addAll(c1); c1.retainAll(c2); 2nd statement will have no practical effect on c1

the answers are 4 and 5.

Its a javabeat question.

I have no idea how to check all these options in a coding way since collection is a new topic for me.please help.


Think of retainAll as an intersection.

c1.retainAll(c2) will only keep those elements from c1 that are also in c2.

So 1 is wrong if c1 and c2 had elements in common.

Option 2 is again suspect because if c1 and c2 had no elements in common, then c1 won't be changed by c1.removeAll(c2).

If c1 and c2 do have elements in common, then those elements in c2 will be removed from c1.

c1.addAll(c2) will not create a new Collection. It will just add all of the elements in c2 to c1.

In option 4, c2.retainAll(c1) will leave in c2 only elements that are also in c1. Then c1.containsAll(c2) will definitely return true.

In option 5, c2.addAll(c1) will add all of the elements of c1 to c2. Then c1.retainAll(c2) won't have any effect because you know that everything in c1 is now in c2.
[ March 13, 2007: Message edited by: Keith Lynn ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
previous post of mine is not printed.i don't know why.that's why i am again sending the same question.


There is something weired with that other "Collection" topic. It's due to a UBB forum software bug I guess - do not worry, it is not your fault.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic