Java HashSet

Java HashSet:
 
  • Syntax:
java.lang.Object
     java.util.AbstractCollection
         java.util.AbstractSet
            java.util.HashSet
  • Java HashSet class is used to create a collection that uses a hash table for storage.
  • Characteristics:
    • HashSet stores the elements by using a mechanism called hashing.
    • HashSet contains unique elements only.
    • HashSet allows null value.
    • HashSet class is non synchronized.
    • HashSet doesn't maintain the insertion order. Here, elements are inserted based on their hashcode.
    • HashSet is the best approach for search operations.
    • The initial default capacity of HashSet is 16, and the load factor is 0.75.
  • Note: A list can contain duplicate elements whereas Set contains unique elements only.
Constructors:
  • HashSet(): It is used to construct a default HashSet.
  • HashSet(int capacity): It is used to initialize the capacity of the hash set to the given integer value capacity. The capacity grows automatically as elements are added to the HashSet.
  • HashSet(int capacity, float loadFactor): It is used to initialize the capacity of the hash set to the given integer value capacity and the specified load factor.
  • HashSet(Collection c): It is used to initialize the hash set by using the elements of the collection c.
Methods:
  • How to create a HashSet object?
    • Syntax: HashSet =new HashSet();
    • Example: HashSet set=new HashSet();
  • How to add new element in HashSet?
    • boolean add(E e):It is used to add the specified element to this set if it is not already present.
    • Example: set.add("One");
  • How to print all elements of HashSet?
    • Example: System.out.println("HashSet elements: " + set);  
  • How to create copy of HashSet?
    • The clone() method of Java HashSet class is used to return a shallow copy of the specified HashSet.
    • Syntax: public Object clone()  
    • Returns: The clone() method returns a shallow copy of this HashSet.
    • Example:
                        HashSet clonedSet = new HashSet();
                        clonedSet = (HashSet)set.clone();
                        System.out.println("The new clone: " + clonedSet);
  • How to check if this HashSet contains the specified element or not?
    • The contains() method of Java HashSet class is used to check if this HashSet contains the specified element or not. It returns true if an element is found otherwise, returns false.
    • Syntax: public boolean contains(Object o)  
    • Note: o: It is the element whose presence in this set is to be tested.
    • Returns: The contains() method returns true if this set contains the specified element.
    • Example: System.out.println("Contains one? :- "+set.contains("one")); 
  • How to check whether HashSet contains an element or not?
    • The isEmpty() method of Java HashSet class is used to check whether HashSet contains an element or not. It returns true if the set contains element, otherwise returns false.
    • Syntax: public boolean isEmpty() 
    • Returns: The isEmpty() method returns true if this set contains no elements.
    • Example: System.out.println("Is the set empty: "+set.isEmpty()); 
  • How to remove the specified element from HashSet?
    • The remove() is a method of Java HashSet class which removes the specified element from this set if it is present.
    • Syntax: public boolean remove(Object o)  
    • Note: o: It is the element whose presence in this set is to be tested.
    • Returns: The remove() method returns true if the set contained the specified element.
    • Example: set.remove("One");
  • How to get the number of elements in this HashSet?
    • The size() method of Java HashSet class is used to get the number of elements in this HashSet (its cardinality).
    • Syntax: Public int size()  
    • Returns: The size() method returns the number of elements present in the HashSet.
    • Example: System.out.println("The size of the set is: " + set.size());
  • How to use iterator method in HashSet?
    • The iterator() method of Java HashSet class is used to return an iterator of the same elements as the HashSet.
    • Syntax: public Iterator iterator()  
    • Returns: The iterator() method returns an Iterator over the elements in this set.
    • Example:
                        Iterator itr=set.iterator();    
                                       while(itr.hasNext()){ 
                                               System.out.println(itr.next()); 
                                         } 
  • How to clear all elements in HashSet?
    • void clear(): It is used to remove all of the elements from the set.
    • Example: set.clear()

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form