Set
Set is an interface in the java.util package.
The most important aspect of Set is: Sets do not contain duplicate elements.
* No two elements in a Set can be equals() to each other.
* At most, one null element may be placed in a Set.
* Some implementations may disallow null.
Set is unordered.
* Subinterfaces may guarantee an order, but Set does not.
Set<E> Is Generic¶
Like List, we tell Set what type of elements it will contain.
Set<String> strSet; // Set of String objects
Set Methods¶
Many methods of Set are the same as List.
* int size() - get the number of elements in the Set.
* boolean isEmpty() - returns true if this Set contains no elements.
* boolean contains(Object o) - returns true if the object is in the Set (such that o.equals(element) is true).
* boolean remove(Object o) - returns true and removes the object if it was in the Set.
Set vs. List¶
Set is different than List, both in its concept and its methods.
-
boolean add(E)This method returns
trueif adding to the set succeeded. * Since aSetdoes not contain duplicates,addonly succeeds when the new element is not.equals()to an element already in theSet. * If a.equals()element was already in theSet, this returnsfalse. -
Setdoes not maintain an order, so there is noget(index)orset(index, E). -
get()does not exist at all.- Reading objects from a
Setrequires anIteratorobject to step through theSet's elements.
- Reading objects from a
iterator()- returns anIterator<E>reference for accessing aSet's elements.
Iterator<E>¶
Set provides an Iterator object, which allows us to step through the elements in the Set.
* boolean hasNext() determines if there are more elements in a collection.
* E next() gets the next element.
Iterator<String> it = strSet.iterator();
while (it.hasNext()) {
String element = it.next();
System.out.println(element);
}