You use them to specify a collection that has no duplicate elements. These interfaces are implemented using the HashSet and TreeSet classes. This example creates a set and then adds some strings to it. The add method returns true. You can also specify a Comparator object to order the set's elements.
Sets are different from lists in that they have no duplicate elements.
Unlike lists, there is no way to manipulate set elements based on their position in the set. For example, you can't retrieve the 17th element of a set by random access, or insert an element before the 59th element of the set. An important concept relating to sets is defining what constitutes a duplicate element. This example leads to an important question. When the program runs, the result is a ClassCastException .