Set

The Set is a special collection that cannot contain duplicate elements. When you want to add an object to a set that already has that object or one, which is equal to the actual one, then the add method will not add the actual object. The add method will return false, indicating the failure.

You can use Set in your program when you need a collection of unique elements where you simply want to check that an element is a member of a set or not, whether an object belongs to a certain group or not.

As we will return to our program code, we will see that the UniqueGuesser class has to implement an algorithm that checks that a color in a guess is present only once. This algorithm is the ideal candidate for a Set to be used:

 private boolean ...

Get Java Projects - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.