Java HashSet tutorial with example program



Class HashSet

All Implemented Interfaces:
Serializable, Cloneable, Iterable, Collection, Set

Direct Known Subclasses:
JobStateReasons, LinkedHashSet
public class HashSet
extends AbstractSet
implements Set, Cloneable, Serializable
{
   //
}
  

Since:
1.2
See Also:
Collection, Set, TreeSet, HashMap, Serialized Form

Constructor Summary
HashSet()
Constructs a new, empty set; the backing HashMap instance has default initial capacity (16) and load factor (0.75).
HashSet(Collection c)
Constructs a new set containing the elements in the specified collection.
HashSet(int initialCapacity)
Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and default load factor (0.75).
HashSet(int initialCapacity, float loadFactor)
Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and the specified load factor.

Method Summary
boolean add(E e)
Adds the specified element to this set if it is not already present.
void clear()
Removes all of the elements from this set.
Object clone()
Returns a shallow copy of this HashSet instance: the elements themselves are not cloned.
boolean contains(Object o)
Returns true if this set contains the specified element.
boolean isEmpty()
Returns true if this set contains no elements.
Iterator iterator()
Returns an iterator over the elements in this set.
boolean remove(Object o)
Removes the specified element from this set if it is present.\
int size()
Returns the number of elements in this set (its cardinality).
Methods inherited from class java.util.AbstractSet
public boolean removeAll(Collection c)
Removes from this set all of its elements that are contained in the specified collection (optional operation). If the specified collection is also a set, this operation effectively modifies this set so that its value is the asymmetric set difference of the two sets.
Methods inherited from class java.util.AbstractCollection
public boolean addAll(Collection c)
Adds all of the elements in the specified collection to this collection (optional operation). The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified collection is this collection, and this collection is nonempty.)

public boolean containsAll(Collection c)
Returns true if this collection contains all of the elements in the specified collection.

public boolean retainAll(Collection c)
Retains only the elements in this collection that are contained in the specified collection (optional operation). In other words, removes from this collection all of its elements that are not contained in the specified collection.
public Object[] toArray()
Returns an array containing all of the elements in this collection. If this collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order.

public T[] toArray(T[] a)
Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this collection.

public String toString()
Returns a string representation of this collection. The string representation consists of a list of the collection's elements in the order they are returned by its iterator, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space). Elements are converted to strings as by String.valueOf(Object).
Methods inherited from interface java.util.Set
boolean addAll(Collection c)
Adds all of the elements in the specified collection to this set if they're not already present (optional operation). If the specified collection is also a set, the addAll operation effectively modifies this set so that its value is the union of the two sets. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress.

public boolean containsAll(Collection c)
Returns true if this collection contains all of the elements in the specified collection.

boolean removeAll(Collection c)
Removes from this set all of its elements that are contained in the specified collection (optional operation). If the specified collection is also a set, this operation effectively modifies this set so that its value is the asymmetric set difference of the two sets.
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait



Related Post

Comments


©candidjava.com