HashSet class Methods in Java

java.util.HashSet

This class implements the Set interface, backed by a hash table. It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the null element.


Some methods of HashSet class.


add(K)This method adds the specified element to this set if it is not already present.


addAll(Collection)This method adds all of the elements in the specified collection to this collection.


clear()This method removes all of the elements from this set.


clone()This method returns a shallow copy of this HashSet instance: the elements themselves are not cloned.


contains(Object)This method returns true if this set contains the specified element.


containsAll(Collection)This method returns true if this collection contains all of the elements in the specified collection.


equals(Object)This method compares the specified object with this set for equality.


forEach(Consumer)This method performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.


hashCode()This method returns the hash code value for this set.


isEmpty()This method returns true if this set contains no elements.


iterator()This method returns an iterator over the elements in this set.


parallelStream()This method returns a possibly parallel Stream with this collection as its source.


remove(Object)This method removes the specified element from this set if it is present.


removeAll(Collection)This method removes from this set all of its elements that are contained in the specified collection.


removeIf(Predicate)This method removes all of the elements of this collection that satisfy the given predicate.


retainAll(Collection)This method retains only the elements in this collection that are contained in the specified collection.


size()This method returns the number of elements in this set (its cardinality).


spliterator()This method creates a late-binding and fail-fast Spliterator over the elements in this set.


stream()This method returns a sequential Stream with this collection as its source.


toArray()This method returns an array containing all of the elements in this set.


toArray(IntFunction)This method returns an array containing all of the elements in this collection, using the provided generator function to allocate the returned array.


toArray(K[])This method returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array.


toString()This method returns a string representation of this collection.


No comments:

Post a Comment