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

Set and HashSet

A Set represents a mathematical set. It is a Collection that, unlike List, does not allow duplicates. There must not be two elements of a Set, say e1 and e2, such that e1.equals(e2). The add method of Set returns false if you try to add a duplicate element. For example, this code prints "addition failed."

Set set = new HashSet();
set.add("Hello");
if (set.add("Hello")) {
    System.out.println("addition successful");
} else {
    System.out.println("addition failed");
}


  

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