• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JQ+ Collections

 
Dominic Mack
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
c2.retainAll(c1);
c1.containsAll(c2); // true or false
Answer True... but why?

This is not "real" code... it is just to get the idea across and verify the concept.

Collection c1 = new Collection ; // could be any collection
Collection c2 = new Collection ; // could be any collection
c1.add("1");
c2.add("2");

c2.retainAll(c1);
// c1 contains ("1")
// c2 contains ()
c1.containsAll(c2);

since c2 contains nothing how can c1 contain the same elements in c2?
 
Nain Hwu
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dominc,
Very interesting question indeed.
I digged out the code containsAll() from AbstractList class, which is superclass for List/Set:

I think it is based on the set theory that
an empty set is contained in any set, including an empty set.

[This message has been edited by Nain Hwu (edited October 16, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic