Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint

15.4. Sets

Unlike other implementations of the Collection interface, implementations of the Set interface do not allow duplicate elements. This also means that a set can contain at most one null value. The Set interface does not define any new methods, and its add() and addAll() methods will not store duplicates. If an element is not currently in the set, two consecutive calls to the add() method to insert the element will first return true, then false. A Set models a mathematical set (see Table 15.3), that is, it is an unordered collection of distinct objects.

Table 15.3. Bulk Operations and Set Logic
Set Methods (a and b are sets)Corresponding Mathematical Operations
a.containsAll(b)ba (subset)
a.addAll(b)a = ab (union)
a.removeAll(b)a = a − b (difference)
a.retainAll(b)a = ab (intersection)
a.clear()a = Ø (empty set)



  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial